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 CenterFile Exchange 中查找有关 Fractals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by