필터 지우기
필터 지우기

RK 4th order for second order ODE

조회 수: 3 (최근 30일)
Abraham
Abraham 2018년 11월 17일
답변: khalida 2023년 7월 14일
Hi, please is there a MATLAB code for RK 4th method 2nd order ODE. I have seen RK 4th for 1st order ODE. what I I need is a code for RK 4th order 2nd ODE (with a function handle). Which I can adjust based on my parameters. Thanks
  댓글 수: 2
Jan
Jan 2018년 11월 17일
It is very easy to convert a 2nd order ODE into a system of 1st order ODEs. Do you have a reason to avoid this?
Abraham
Abraham 2018년 11월 18일
Figured it out. Thanks, Jan.

댓글을 달려면 로그인하십시오.

답변 (1개)

khalida
khalida 2023년 7월 14일
clear,clc
% % % % % RK4 system of odes higher order
h = 0.1;
x_beg = 0;
x_end = 0.7;
y_initial= 0;
z_initial= 0;
F_xy=@(x,z)(z);
F_xz=@(x,y,z)(-((1+5*x)/2*x*(x+1))*z-y^3+5*x+11*x^2+(8/27)*x^9);
x=x_beg:h:x_end;
y=zeros(1,length(x));
y(1)=y_initial;
z=zeros(1,length(x));
z(1)=z_initial;
for i=1:(length(x)-1);
i=1:(length(x)-1);
k1 = F_xy(x(i),y(i),z(i));
L1 = F_xz(x(i),y(i),z(i));
k2 = F_xy((x(i)+h/2),(y(i)+0.5*h*k1),z(i)+0.5*h*L1);
L2 = F_xz((x(i)+h/2),(y(i)+0.5*h*k1),z(i)+0.5*h*L1);
k3 = F_xy((x(i)+h/2),(y(i)+0.5*h*k2),z(i)+0.5*h*k2);
L3 = F_xz(x(i)+h/2,(y(i)+0.5*h*k2),(z(i)+0.5*h*L2));
k4 = F_xy((x(i)+h),(y(i)+k3*h),z(i)+L3*h);
L4 = F_xz((x(i)+h),(y(i)+k3*h),(z(i)+L3*h));
y(i+1) = y(i) + (1/6)*(k1+2*k2+2*k3+k4)*h;
z(i+1) = z(i) + (1/6)*(L1+2*L2+2*L3+L4)*h;
end
% plot(y,z)
figure(1)
plot(x,z)
hold on
plot(x,y)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by