I can't plot.
Show older comments
for t=0:10
disp(t);
x=-.196*t;
disp(x);
c=-40:20:20;
disp(c)
v=50+(c*exp(x));
disp(v);
end
plot(t,v)
1 Comment
Answers (1)
Val Kozina
on 14 Aug 2017
In your code, 't' and 'v' are just scalars, not vectors. At the end of the loop, 't' will simply have the value 10, and 'v' will be the last calculated value. If you are trying to plot the values of 'v' for each 't' from 0 to 10, you should consider declaring these variables as vectors before the loop. You can also declare 'c' outside of the loop since its value never changes, allowing you to preallocate the right amount of space for 'v'. Then, simply iterate over the values in vector 't', and record the result into the next spot in 'v' (note that since matrix indices must start at 1 you will have to index using the current value of t + 1). Altogether your code would look like this:
t = 0:10;
c=-40:20:20;
v = zeros(numel(t), numel(c));
for cur_t = t
disp(cur_t);
x=-.196*cur_t;
disp(x);
v(cur_t+1, :)=50+(c*exp(x));
end
plot(t,v)
This should result in 4 separate lines on a single plot, one for each value of c.
1 Comment
Paul Dedrich
on 14 Aug 2017
Thank you. Perfect fix.
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!