# How could I use display function for this?

2 views (last 30 days)
Hdez on 14 Jan 2021
Commented: Walter Roberson on 14 Jan 2021
Hello there, I am using Matlab but it is not my thing at all so I have minimal knowledge about it. I would like to display the result of dt*t but I don't know how to word it properly on the display function. Can someone help me please?

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
ans =
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
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))
ans = '[0 0.25 0.38 0.46 0.52 0.57 0.61 0.65 0.68 0.71 0.73 0.75 0.78 0.8 0.81 0.83 0.85 0.86 0.87 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1 1.01 1.01 1.02 1.03 1.04 1.04 1.05 1.06 1.06 1.07 1.08 1.08 1.09 1.09 1.1 1.1 1.11 1.11 1.12 1.12]'
converge_idx = find(diff(s) < 0.05, 1, 'first')
converge_idx = 6
s(converge_idx)
ans = 0.5708
converge_time = (converge_idx-1) * dt
converge_time = 1.2500
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.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by