MATLAB: How to increment variables to store new values

increment variablesstoring multiple variables

clear;clc;
i = 0;
f = 60;
L = 30;
E = (1.25*10^8);
I = 0.05;
z0 = 0;
u0 = 0;
R = f/(2*E*I);
for h = 1:1:30
k1(i+1) = u0;
l1 = R * (L - z0)^2;
k2 = u0+(h/2)*l1;
l2 = (R)*(L- (z0 +(h/2)))^2;
k3 = u0 +l2/2;
l3 = (R)*(L - (h/2))^2;
k4 = u0 + h*(l3);
l4 = R* (L - h)^2;
y(i+1) = u0 + (h/6)*(k1+2*(k2+k3)+k4);
z(i+1) = z0 +(h/6)*(l1+2*(l2+l3)+l4);
y_true = R * (L - h)^2;
Error = (abs((y_true - y(i+1)))/y_true)*100;
end
I am trying to store the values of y for each value of h and am not sure what i should be doing for the syntax of it. Any help would be greatly appreciated!

Best Answer

If you want to store y for each h, then you should be storing into y(h) and z(h) instead of into y(i+1) and z(i+1), and you would access y(h) instead of y(i+1) in calculating the Error.

What you had would work (but not be the best solution) if you incremented i after each iteration of the "for h" loop.

Note: you are not storing each individual Error value as you go, so it is only going to reflect the final h value, and so would not need to be calculated inside the loop if you only need the final value.