% the program takes the initial data y_0 at the initial time x_0 and 
% computes up to time x_final using N timesteps of size dx.  It creates
% two vectors, x and y, which you can then plot against each other
% plot(x,y)
%
% To call the routine, use the command:
%
% [x,y] = adams_bashforth_2(y_0,x_0,x_final,N,@f)
%
function [x,y] = adams_bashforth_2(y_0,x_0,x_final,N,f)

% step-size:
dx = (x_final-x_0)/N;

% for dy/dx = f(x,y(x))
%
% y(x+dx) ~ y(x) + dx/2*(3*f(x,y(x))-f(x-dx,y(x-dx)))

% pux initial data into vector
x(1) = x_0;
y(:,1) = y_0;
% take an Euler step to compute y(x+dx)
x(2) = x_0 + dx;
F_now = feval(f,x(1),y(:,1));
y(:,2) = y(:,1) + dx*F_now;
for ii=2:N
  F_old = F_now;
  F_now = feval(f,x(ii),y(:,ii));
  y(:,ii+1) = y(:,ii) + dx/2*(3*F_now - F_old);
  x(ii+1) = x_0 + ii*dx;
end




