function [rkmidT, rkmidW] = rkmid_2(Fun, t0, tlast, w0, N)
%%%%%%%%%%% Runge-Kutta Midpoint 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 
% 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:
% rkmidT  - time points (column-vector).
% rkmidW  - Solution column-vector per rkmidT-values.
%
% The result can be displayed by: plot(rkmidT, rkmidW).

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

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