% the program takes the intial 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.
% The ODE you're trying to solve is dy/dx = f(x,y) where the vector
% field is contained in the subroutine f.m
%
% To call the routine, use the command:
%
% [x,y] = richardson_extrap_euler_step_2(y_0,x_0,x_final,N,@f)

function [x,y] = richardson_extrap_euler_step_2(y_0,x_0,x_final,N,f)

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

% put initial data into vector
x(1) = x_0;
y(:,1) = y_0;
for ii=1:N
  F1 = feval(f,x(ii),y(:,ii));
  % take one big step to reach y1
  y1 = y(:,ii) + dx*F1;
  % take two small steps to reach y3
  y2 = y(:,ii) + dx/2*F1;
  F2 = feval(f,x(ii)+dx/2,y2);
  y3 = y2 + dx/2*F2;
  % use Richardson extrapolation
  y(:,ii+1) = 2*y3-y1;
  x(ii+1) = x_0 + ii*dx;
end
