Posted By:
31 Mar, 2013 8:44 pm PDT

4th order Runge Kutta method to solve an ODE in matlab

I have an ordinary differential equation dy/dx = -2x-y, with initial condition on y (x = 0) = -2, when x = 0

I want to numerically solver this ODE using 4th order Runke-Kutta Method in Matlab with a step size of 0.1.

Attachment:None


 
clc
clear all
close all
% y? =?2x?y
% initial condition
x(1) = 0;
y(1) = -2;
h = 0.1;% step size

%RK4
%%%%%%%%%%%%%%
for i = 1:10
    k1 = h*(-2*x(i)-y(i));
    k2 = h*(-2*(x(i)+0.5*h)-(y(i)+0.5*k1));
    k3 = h*(-2*(x(i)+0.5*h)-(y(i)+0.5*k2));
    k4 = h*(-2*(x(i)+h)-(y(i)+k3));
    y(i+1) = y(i)+1/6*(k1+k2+k3+k4);
    x(i+1) = x(i)+h;
end

hold on
plot(x,y,'+-', 'Linewidth', 1.5, 'color', 'blue')
xlabel('x')
ylabel('y')
legend('RK4')

 Please watch the video and attached .m file for details**********

Attachment: RK4.m
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud