Using a previously calculated value in a for loop

I am attempting to calculate the volume over time of a basin of water given the inflow and outflow rates. With this, I am attempting to code a for loop that uses the previous calculated of the volume as the volume in the next iteration. The code I have written does not do this, it causes the loop to run continuously. the code I've attempted to make work is this: clear all; close all v=200; t=0:10;
for t<=240 vout=v+t.*(10-.1.*v) v=vout end
Any help would be greatly appreciated.

2 个评论

Please use the toolbar button
to put your code in a readable font.
clear all; close all
v=200;
t=0:10;
for t<=240
vout=v+t.*(10-.1.*v)
v=vout
end

请先登录,再进行评论。

回答(1 个)

You have not written a valid MATLAB for loop. I'm not sure what you are trying to do because you create t as a vector and then don't index in the for loop. Then you try to test t <=240 but t never goes beyond 10. So I just have to guess at what you're trying to do with the for loop
v = 200;
for t = 1:240
vout(t) = v+t*(10-.1*v);
v = vout(t);
end

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

提问:

2013-9-29

编辑:

2013-9-29

Community Treasure Hunt

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

Start Hunting!

Translated by