Runga Kutta Mthod 4

7 views (last 30 days)
Ali
Ali on 13 Nov 2020
Edited: Alan Stevens on 14 Nov 2020
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);

Accepted Answer

Alan Stevens
Alan Stevens on 14 Nov 2020
Edited: Alan Stevens on 14 Nov 2020
As before, your value for h is too large. Set h = 0.01 and it works!

More Answers (0)

Categories

Find more on Fractals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!