MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by 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!!!

*No products are associated with this question.*

Answer by Matt Fig
on 16 Mar 2011

Accepted answer

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;

Oleg Komarov
on 16 Mar 2011

I would go w/o the loop:

nt = -la*timestep*na + na;

plot(t,nt,'- dr','markerf','g');

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.

Answer by 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();

Answer by 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:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/colon.html

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.

## 2 Comments

## Stan (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/3288#comment_6714

0.0625* for the last value of timestep.. sorry typo =)

## Stan (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/3288#comment_6715

and in the for loop,

its supposed to be plot(t(n),nt)