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일

2 개 추천

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일

댓글:

2017년 8월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by