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

24 ビュー (過去 30 日間)
Esat Akdöngel
Esat Akdöngel 2022 年 1 月 17 日
回答済み: Torsten 2022 年 1 月 17 日
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

回答 (2 件)

VBBV
VBBV 2022 年 1 月 17 日
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 件のコメント
Esat Akdöngel
Esat Akdöngel 2022 年 1 月 17 日
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.

サインインしてコメントする。


Torsten
Torsten 2022 年 1 月 17 日
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.

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by