clear all
% Define the problem
f = @(t,y) -2*t*y; 
t0 = 0; 
tend = 1; 
y0 = 1; 
yend = exp(-tend^2);

% Store error and stepsizes
err = [];
h = [];

N = 5;    % Initial number of steps
M = 10;    % Times the stepsize is halfed.
for i=1:M
    [t,y] = rk(f,t0,tend,y0,N);
    
    hi = (tend-t0)/N; 
    erri = yend-y(end); 
    disp(sprintf('  h=%8.6f,    err=%9.6f',hi, erri)); 
    
    err = [err,erri];
    h = [h,hi];
    N=N*2;
end

loglog(h,abs(err),'og'); 
grid on
title('Error of Eulers method'); 
xlabel('h'); 
ylabel('|Error|')
hold on
 
pp = polyfit(log(h),log(abs(err)),1); 
disp(sprintf('\n Order = %5.3f',pp(1)));
loglog(h,0.2*h,'--r')
 
hold off







