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 comentarios

Matt J
Matt J el 29 de Sept. de 2013
Editada: Matt J el 29 de Sept. de 2013
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

Iniciar sesión para comentar.

Respuestas (1)

Wayne King
Wayne King el 29 de Sept. de 2013
Editada: Wayne King el 29 de Sept. de 2013
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

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 29 de Sept. de 2013

Editada:

el 29 de Sept. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by