function [t,y] = Heun(f,tspan,y0,nstep)

 if(size(y0,2)>1) y0=y0'; end %Column vector
 t0= tspan(1); tn = tspan(2); 
 h = (tn-t0)/nstep;
 t = [t0:h:tn];
 nOfUnk = length(y0);
 y = [y0';zeros(nstep,nOfUnk)];
 yi = y0;
 for i=1:nstep
   ti = t0 + (i-1)*h;
   k1 = f(ti,yi);
   k2 = f(ti+h,yi+h*k1);
   yi = yi + (k1+k2)*h/2;
   y(i+1,:)=yi';
  end
