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;
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
No products are associated with this question.
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;
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
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: