35 views (last 30 days)

Stephan Ciobanu
on 14 Jan 2021

% Hi!

% if dt*t gives you a numeric array

% you can use:

disp(num2str(dt*t))

% or

fprintf('My result is dt*t = %6.5f \n',dt*t)

% you can also use: frpinf, print... type:

help fprintf

% or

help disp

% or

help print

Walter Roberson
on 14 Jan 2021

dt*t would seldom give time as a result.

dt is usually rate of time change, either as an infinitesimal or as a time change per unit (such as for each array element.) Time change per unit would be like "seconds per entry".

t is usually time, such as in seconds.

Multiply "seconds per entry" by "seconds" and you would get seconds squared per entry, which is not a time.

Now, if dt is "seconds per entry" units and instead of being time, t is number of entries (array index rather than time), then that could make sense -- seconds per entry times number of entries would give seconds as a result.

Walter Roberson
on 14 Jan 2021

If you mean the derivative as in then MATLAB does not have any direct way to display that in any kind of nice format. All it has is multiplication by a variable that happens to be named dt as in

syms t dt

t*dt

Notice there is a risk that the order will get rearranged.

If you were using Live Script, then you would be able to "insert equation" that included latex format, but that kind of formatting is only static text documenting what you are doing, and never the output of a result.

MATLAB has a latex() function that can accept a symbolic expression and create corresponding latex code, but it currently has no way to display that latex, not even in Live Script.

Walter Roberson
on 14 Jan 2021

Walter Roberson
on 14 Jan 2021

Suppose that I calculate the temperature of the plate at 1/4 second intervals for 2 seconds. So dt is 1/4 second per entry, and I have entries for 0, 1/4, 1/2, 3/4, 1, 5/4, 3/2, 7/4, 2 seconds. Suppose convergence was found at that point, so t = 2 seconds. dt = 1/4 second per entry, t = 2 seconds. Multiply the two of them and you get 1/2 second / entry * seconds = 1/2 seconds^2 / entry as the units. That is not the convergence time -- the convergence time is 2 seconds.

So if t is time, then use it directly.

But perhaps instead of t being time, it is array index, like

dt = 1/4;

s = 0;

for t = 1 : 50

s(t+1) = s(t) + dt/t;

end

mat2str(round(s,2))

converge_idx = find(diff(s) < 0.05, 1, 'first')

s(converge_idx)

converge_time = (converge_idx-1) * dt

Here t is acting as an array index, and is not a time itself. Assuming time starting at 0, time at convergence can be calculated by knowing how many steps had to be taken (which is index minus 1) and multiply by the timestep.

This is not a matter of being an expert at MATLAB, this is fundamental units analysis: time multiplied by change in time per step does not give time as a result, but number of steps multiplied by change in time per step does give time as a result.

If t actually represents number of steps in your code, then I would recommend using a different variable name, as people are too likely to confuse it as being time itself rather than step number.

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 0 Comments

Sign in to comment.