# For Loop Using the Previous Calculated Term for Each Iteration

9 views (last 30 days)
Michaela Wood on 24 Sep 2020 at 16:47
Answered: David Hill on 24 Sep 2020 at 17:15
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

David Hill on 24 Sep 2020 at 17:15
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);