I can't plot.
显示 更早的评论
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 个评论
回答(1 个)
Val Kozina
2017-8-14
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!