% 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] = runge_kutta_2(y_0,x_0,x_final,N,@f)
%
function [x,y] = runge_kutta_2(y_0,x_0,x_final,N,f)

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

gamma2 = 3/4;
gamma1 = 1-gamma2;
alpha = 1/(2*gamma2);
beta = 1/(2*gamma2);

% for dy/dx = f(x,y(x))
%
% first define 
%         F1 = f(x,y(x))
% then 
%         F2 = f(x+alpha*dx,y(x)+beta*dx*F1)
% y(x+dx) ~ y(x) + dx*(gamma1*F1+gamma2*F2);

% pux initial data into vector
x(1) = x_0;
y(:,1) = y_0;
for ii=1:N
  F1 = feval(f,x(ii),y(:,ii));
  F2 = feval(f,x(ii)+alpha*dx,y(:,ii)+beta*dx*F1);
  y(:,ii+1) = y(:,ii) + dx*(gamma1*F1 + gamma2*F2);
  x(ii+1) = x_0 + ii*dx;
end




