function [heunT, heunW] = heun_2(Fun, t0, tlast, w0, N)
%%%%%%%%%%% Runge-Kutta Heun Method for Initial Value Problems %%%%%
%% 
%% Solve y'= f(t,y),   a<= t <=b,  y(a)=  alpha
%%
%% It is assumed that you have the function f(t,y) defined elsewhere 
%% as a function in a .m file
% INPUT:
% Fun   - String containing name of function m-file (enclose in '  ')
% t0    - Initial value of t.
% tlast - Last value of t.
% w0    - Initial value.
% N     - The number of steps
%
% OUTPUT:
% heunT  - time points (column-vector).
% heunW  - Solution column-vector per heunT-values.
%
% The result can be displayed by: plot(heunT, heunW).

if nargin < 5, 
 error('Not enough arguments, enter help heun_2 for help')
end;
% Initialization
heunT = t0;
h = (tlast - t0)/N;
heunW = w0;
t=t0;w=w0;

%% Step 2
for i=1:N;
 k1= h*feval(Fun,t,w);  
 w = w + (k1/4)+(3*h/4)*feval(Fun, t+(2*h/3), w+2*(k1/3)); %%Step 3
 t = t0 + i*h;                      %%Step 3
 heunT=[heunT;t]; heunW = [heunW;w];               %%Step 4
end; 
