For Loop Using the Previous Calculated Term for Each Iteration

9 views (last 30 days)
Hello. I feel like this should not be too difficult but I am sturggling with it.. Im trying to find the value calculated with an equation every minute, up to 15 minutes (equations uses seconds). The first equation starting at 0 seconds using a constant value already given (denoted as 'r_NaCl') but every other iteration will use the previously calculated value in the place of the r_NaCl. This was my try but it is not working. I then need to plot it which I can do but this attemp gives me an x1 that is 1x16 and y1 that is 1x31 and im not sure why. Thank you in advance for your help!
x1=[];
y1=[];
for time=0:60:900
if time==0;
rate_NaCl=sqrt(((r_NaCl)^2)+(2*rdrdt_NaCl*time));
elseif time>0;
rate_NaCl(i)=sqrt(((rate_NaCl(i-1))^2)+(2*rdrdt_NaCl*time));
end
x1=[x1 time];
x2=[x2 time];
end

Accepted Answer

David Hill
David Hill on 24 Sep 2020
t=0:60:900
rate_NaCl(1)=r_NaCl;
for k=2:length(t)-1
rate_NaCl(k)=sqrt(rate_NaCl(k-1)^2 + 2*rdrdt_NaCl*t(k));
end
plot(t,rate_NaCl);

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!