How can I plot a second order differential equation with boundary condition using fourth order Runge-Kutta method?

조회 수: 8 (최근 30일)
%%%%%%%%%%%%%%%% Runga-Kutta%%%%%%%%%%%%%%%%
h=0.0001;
xfinal=d;
x(1)=0;
y(1)=0; % initial value of y
y(xfinal)=0; % final value of y
% Let y' = z (f1) and y" = z' (f2);
f1 = @(x, y, z) z;
f2 = @(x, y, z) ky^2*y-(ky*(-2*W*(pi/d)*tan(2*pi*x/d)+2*u0*((pi/d)^2)*cos(2*pi*x/d))*y)/(OP3-ky*u0*(sin(pi*x/d).^2-1/2)+...
B*(OP3-ky*u0*(sin(pi*x/d).^2-1/2)-A*(-2*W*(pi/d)*tan(2*pi*x/d)+2*u0*((pi/d)^2)*cos(2*pi*x/d)-ky*(OP3-ky*u0*(sin(pi*x/d).^2-1/2))))*(1-...
M*(opi^2)/(M*OP3^2-gi*Ti*ky^2)));
for i=1:ceil(xfinal/h)
x(i+1)=x(i)+h;
K1y = f1(x(i), y(i), z(i));
K1z = f2(x(i), y(i), z(i));
K2y = f1(x(i)+0.5*h, y(i)+0.5*K1y*h, z(i)+0.5*K1z*h);
K2z = f2(x(i)+0.5*h, y(i)+0.5*K1y*h, z(i)+0.5*K1z*h);
K3y = f1(x(i)+0.5*h, y(i)+0.5*K2y*h, z(i)+0.5*K2z*h);
K3z = f2(x(i)+0.5*h, y(i)+0.5*K2y*h, z(i)+0.5*K2z*h);
K4y = f1(x(i)+h, y(i)+K3y*h, z(i)+K3z*h);
K4z = f2(x(i)+h, y(i)+K3y*h, z(i)+K3z*h);
y(i+1) = y(i)+(K1y+2*K2y+2*K3y+K4y)*h/6;
z(i+1) = z(i)+(K1z+2*K2z+2*K3z+K4z)*h/6;
end
plot(x,y,'-','linewidth',1)
hold on
  댓글 수: 1
John D'Errico
John D'Errico 2023년 3월 17일
It looks like you already solved the ODE, and plotted it. Where is the problem? (Even so, if this were not homework, as it surely is, you should be using an ODE solver, not writing your own code.)

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

답변 (1개)

Cameron
Cameron 2023년 3월 17일
Here is a list of built-in ODE solvers within MATLAB.

카테고리

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