Index exceeds the number of array elements. Index must not exceed 1.

26 views (last 30 days)
Esat Akdöngel
Esat Akdöngel on 17 Jan 2022
Answered: Torsten on 17 Jan 2022
I'm trying to apply runge kutta method to my problem but i messed up the code at line34(k1_ivme) and matlab giving this error. What should i do for fix this?
z(1)=-314;
v(1)=0;
h=0.1;
tson=10;
t=0:h:tson;
L=1.5;
vi=0.000001;
n = 100;
for i=1:n-1;
Re(i)=abs(v(i))*L/vi;
Cf(i)=0.075/((log(Re)-2).^2);
k1_hiz(i)=h*v(i);
k1_ivme=h*(5207.3-3.47*Cf(i)*v(i)^2);
k2_hiz(i)=h*(v(i)+0.5*k1_ivme(i));
k2_ivme(i)=h*(5207.3-3.47*(Cf(i)+0.5*k1_ivme)*(v(i)+0.5*k1_ivme))^2;
k3_hiz(i)=h*(v(i)+0.5*k2_ivme(i));
k3_ivme(i)=h*(5207.3-3.47*(Cf(i)+0.5*k2_ivme)*(v(i)+0.5*k2_ivme))^2;
k4_hiz(i)=h*(v(i)+k3_ivme(i));
k4_ivme(i)=h*(5207.3-3.47*(Cf(i)+k3_ivme)*(v(i)+k3_ivme))^2;
z(i+1)=z(i)+(1/6*(k1_hiz(i)+2*k2_hiz(i)+2*k3_hiz(i)+k4_hiz(i)));
v(i+1)=v(i)+(1/6*(k1_ivme(i)+2*k2_ivme(i)+2*k3_ivme(i)+k4_ivme(i)));
end

Answers (2)

VBBV
VBBV on 17 Jan 2022
z(i+1)=z(i)+(1/6*(k1_hiz(i)+2*k2_hiz(i)+2*k3_hiz(i)+k4_hiz(i)));
v(i+1)=v(i)+(1/6*(k1_ivme(i)+2*k2_ivme(i)+2*k3_ivme(i)+k4_ivme(i)));
Look at these two lines. you have declared n as 100 and trying to access 101th element . Could that be possible ?
  4 Comments
Esat Akdöngel
Esat Akdöngel on 17 Jan 2022
i think i try to find v(i) values but the problem is they need for the calculation of Re and Cf before matlab calculate them.

Sign in to comment.


Torsten
Torsten on 17 Jan 2022
z(1)=-314;
v(1)=0;
h=0.1;
tson=10;
t=0:h:tson;
L=1.5;
vi=0.000001;
n = 100;
for i=1:n
Re(i)=abs(v(i))*L/vi;
Cf(i)=0.075/((log(Re(i))-2).^2);
k1_hiz(i)=h*v(i);
k1_ivme(i)=h*(5207.3-3.47*Cf(i)*v(i)^2);
k2_hiz(i)=h*(v(i)+0.5*k1_ivme(i));
k2_ivme(i)=h*(5207.3-3.47*(Cf(i)+0.5*k1_ivme(i))*(v(i)+0.5*k1_ivme(i)))^2;
k3_hiz(i)=h*(v(i)+0.5*k2_ivme(i));
k3_ivme(i)=h*(5207.3-3.47*(Cf(i)+0.5*k2_ivme(i))*(v(i)+0.5*k2_ivme(i)))^2;
k4_hiz(i)=h*(v(i)+k3_ivme(i));
k4_ivme(i)=h*(5207.3-3.47*(Cf(i)+k3_ivme(i))*(v(i)+k3_ivme(i)))^2;
z(i+1)=z(i)+(1/6*(k1_hiz(i)+2*k2_hiz(i)+2*k3_hiz(i)+k4_hiz(i)));
v(i+1)=v(i)+(1/6*(k1_ivme(i)+2*k2_ivme(i)+2*k3_ivme(i)+k4_ivme(i)));
end
Now the code runs, but there must be something wrong with your RK4 integrator.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by