Runga Kutta Mthod 4

7 ビュー (過去 30 日間)
Ali
Ali 2020 年 11 月 13 日
編集済み: Alan Stevens 2020 年 11 月 14 日
I want to solve second order equation through Ruga kutta method .Please check what is error in this code
%input paramters
m=100;
k=25000;
e=100;
F=15000;
w=(k/m)^0.5;
t(1)=0;
x(1)=0;
v(1)=1;
h=0.1;
tfinal=100;
N=ceil((tfinal-t(1))/h);
f1=@(t,x,v) v;
f2=@(t,x,v) -(k/m)*v-(e/m)*x+(F/m)*cos(w*t);
for j=1:N;
t(j+1)=t(j)+h;
K1x=f1(t(j),x(j),v(j));
K1v=f2(t(j),x(j),v(j));
K2x=f1(t(j)+h/2,x(j)+h/2*K1x,v(j)+h/2*K1v);
K2v=f2(t(j)+h/2,x(j)+h/2*K1x,v(j)+h/2*K1v);
K3x=f1(t(j)+h/2,x(j)+h/2*K2x,v(j)+h/2*K2v);
K3v=f2(t(j)+h/2,x(j)+h/2*K2x,v(j)+h/2*K2v);
K4x=f1(t(j)+h,x(j)+h*K3x,v(j)+h*K3v);
K4v=f2(t(j)+h,x(j)+h*K3x,v(j)+h*K3v);
x(j+1)=x(j)+h/6*(K1x+2*K2x+2*K3x+K4x);
v(j+1)=v(j)+h/6*(K1v+2*K2v+2*K3v+K4v);
end
disp(x(N));
figure;
plot(t,x,'lineWidth',1);

採用された回答

Alan Stevens
Alan Stevens 2020 年 11 月 14 日
編集済み: Alan Stevens 2020 年 11 月 14 日
As before, your value for h is too large. Set h = 0.01 and it works!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFractals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by