Matlab question in connecting dots..?

1 view (last 30 days)
Stan
Stan on 16 Mar 2011
tfinal = 75;
hla = 1; hlb = 5; la = log(2)/hla; lb = log(2)/hlb;
n0 = 100 ; timestep = 1;
t= linspace(0,75,76); na = n0*exp(-la*t); na1 = diff(na)/timestep; na1ex = -la*na;
for n=1:75
nt = -la*timestep*na(n) + na(n)
hold on
lineplot(t(n),nt)
end
------------------------------------- i have absolutely no idea in using matlab.. i just started using it today for my nuclear engineering project.. (i honestly dont know how i came up with this code.. i copied and pasted from many different codes)
The current question/help that i need is when i plot the graph using for loop, i get a bunch of points... (the values of the points are correct) i needd to connect them in sequential order.. how do i do this???
the other question is, if i wanted to graph the SAME graph generated by the for loop, but with a different timestep value, on the same graph, how wud i do this? the values for tthe timesteps r 1, 0.5 0.25 0.125 0.625
thx!!!
  2 Comments
Stan
Stan on 16 Mar 2011
0.0625* for the last value of timestep.. sorry typo =)
Stan
Stan on 16 Mar 2011
and in the for loop,
its supposed to be plot(t(n),nt)

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 16 Mar 2011
Try this instead:
tfinal = 75;
hla = 1;
hlb = 5;
la = log(2)/hla;
lb = log(2)/hlb;
n0 = 100 ;
timestep = 1;
t= 0:timestep:tfinal;
na = n0*exp(-la*t);
na1 = diff(na)/timestep;
na1ex = -la*na;
nt = zeros(1,length(t));
for n=1:length(t)
nt(n) = -la*timestep*na(n) + na(n);
end
plot(t,nt)
% FOR loop could be replaced by: nt2 = -la*timestep*na + na;
  3 Comments
Walter Roberson
Walter Roberson on 16 Mar 2011
Not having to connect the dots is better, but connecting the dots is a common request, especially for people doing real-time plotting.
Matt Fig
Matt Fig on 16 Mar 2011
@Oleg, I did manage to squeeze that it at the end...

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 16 Mar 2011
Before the loop:
h = line([],[]);
Inside the loop, remove the "hold on" and the lineplot() call, and replace the pair of them with:
set(h, 'XData', [get(h, 'XData') t(n)], ...
'YData', [get(h, 'YData') nt] );
If you want to see the line grow through each iteration, then right after that line add
drawnow();

Laura Proctor
Laura Proctor on 16 Mar 2011
Since MATLAB is vectorized, you actually don't need to use a for loop to perform vector addition, so instead of the FOR loop that you have written, you can just use the following lines of code:
nt = -la *timestep * na + na;
plot(t,nt,'.-')
Use the HOLD ALL command after plot and the color selection will toggle automatically.
When you create your time vector, it would be easier to use the colon operator rather than LINSPACE because you know the value of the timesteps. For example, for a timestep of 0.5 use:
t = 0:0.5:75;
which creates a vector from 0 to 75 with spacing of 0.5:
  1 Comment
Walter Roberson
Walter Roberson on 16 Mar 2011
A step of 0.5 from a base that is an integer will work to create a vector with spacing 0.5. However, if the step is not an integral multiple of 2^(-N) for integral N, then your vector spacing is going to be irregular. Using linspace() will get you a more accurate spacing in such cases, but it will still not be quite right in some cases.
Anyhow, linspace() is usually better than the colon operator.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!