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

3rd/4th order Adams-Bashforth method to solve an ODE in matlab

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

I want to numerically solver this ODE using 3rd order Adams-Bashforth (AB) method in Matlab with a step size of 0.1. Also how can I solve it using 4th order AB method

Attachment:None


clc
clear all
close all

% initial condition
x(1) = 0;
y(1) = -2;
h = 0.1;% step size


%Adam-bashforth 3rd method
%%%%%%%%%%%%%%
for i = 1:10
    f(i) = (-2*x(i)-y(i));
    if(i<3)
        y(i+1) = y(i)+h*(-2*x(i)-y(i));
    else
        y(i+1) = y(i)+h/12*(23*f(i)-16*f(i-1)+5*f(i-2));

    end
    x(i+1) = x(i)+h;
end

% plot
figure
set(gca,'Fontsize',15)
plot(x,y,'+-', 'Linewidth', 1.5)
xlabel('x')
ylabel('y')




%ADAM-bashforth 4th method
%%%%%%%%%%%%%%
% yn+1 = (yn -2hxn+1)/(1+h)
for i = 1:10
    f(i) = (-2*x(i)-y(i));

    if(i<4)
        y(i+1) = y(i)+h*(-2*x(i)-y(i));
    else
        y(i+1) = y(i)+h/24*(55*f(i)-59*f(i-1)+37*f(i-2)-9*f(i-3));

    end
    x(i+1) = x(i)+h;
end

% plot
hold on
plot(x,y,'+-', 'Linewidth', 1.5, 'color', 'red')
xlabel('x')
ylabel('y')
legend('3rd order','4th order')

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

Attachment: adams.m
Was it useful?  




Please login to reply to this problem.