Difficulty plotting values from a for loop
5 views (last 30 days)
I'm attempting to create a plot from values generated in a for loop, but the values don't seem to be appearing in the plot that MATLAB generates. Here is the code:
for t = -5:1:50;
if t>=0 && t<=8;
v = 10.*t.^2 - 5.*t;
elseif t>8 && t<16;
v = 624 - 3.*t;
elseif t>=16 && t<=26;
v = 36.*t + 12.*(t - 16).^2;
v = (2136).*exp(-0.1.*(t - 26));
v = 0;
It seems to be only plotting the last point (at t = 50) and none of the previous points, which has led me to believe that every time the file goes through the loop and reaches the line where it is supposed to plot the coordinate pair, it overwrites the previous pair with the current one, and does so until the end, thus leaving me with only the final coordinate point on the plot. I've tried putting "disp(t)" and "disp(v)" in the place of "plot(t,v,'o')", and all the corresponding values appear in the command box, but only the last one ends up being actually plotted. How can I go about changing the code so that it retains each of the "v" values as "t" goes from -5 to 50, and is able to plot the entire domain? Do I perhaps need to change the variables in the line where it plots so that they aren't simply "t" and "v"?
TallBrian on 4 Oct 2016
Yes, your graph is redrawing each time through the loop. You can add the command
after the plot and then the next plot will be added to the figure instead of replacing the existing figure.