Clear Filters
Clear Filters

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

4 views (last 30 days)
%%%%%%%%%%%%%%%% 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 Comment
John D'Errico
John D'Errico on 17 Mar 2023
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.)

Sign in to comment.

Answers (1)

Cameron
Cameron on 17 Mar 2023
Here is a list of built-in ODE solvers within MATLAB.

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!