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

% for dy/dx = f(x,y(x))
% y(x+dx) ~ y(x) + dx y'(x) + dx^2/2 y''(x)
%         = y(x) + dx f(y(x),x) + dx^2/2 ( f_y(x,y(x)) f(x,y(x)) + f_x(x,y(x))

% 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));
  dF1 = feval(df,x(ii),y(:,ii));
  y(:,ii+1) = y(:,ii) + F1*dx;
  y(:,ii+1) = y(:,ii+1) + dF1*dx^2/2;
  x(ii+1) = x_0 + ii*dx;
end
