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 个评论

Rik
Rik 2017-8-14
What are you trying to do? There are many thing that could be wrong with this code.
Add some detail to this question, show what you tried in a readable way: use the {}Code button.
Have a read here and here. It will greatly improve your chances of getting an answer.

请先登录,再进行评论。

回答(1 个)

Val Kozina
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 的更多信息

标签

提问:

2017-8-14

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by