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 Comments

Matt J
Matt J on 29 Sep 2013
Edited: Matt J on 29 Sep 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

Sign in to comment.

Answers (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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 29 Sep 2013

Edited:

on 29 Sep 2013

Community Treasure Hunt

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

Start Hunting!