
t0=0;
T=1;
y0=1;

f1=@(t,x) t;
sol1=@(t) 1+t.^2/2;

err=zeros(6,3);
for k=1:6 % cycle to repeat the experiments with different h
    h=0.1*2^(-k+1);% change h
    [t,y]=e614(t0,T,h,f1,y0); % call to function
%     figure
%     t(end)
    err(k,1)=abs(y(end)-sol1(t(end))); % calculate the error in the end point
end

f2=@(t,x) (t.^2).*x;
sol2=@(t) exp(t.^3/3);

for k=1:6
    h=0.1*2^(-k+1);
    [t,y]=e614(t0,T,h,f2,y0);
%     figure
%     t(end)
    err(k,2)=abs(y(end)-sol2(t(end)));
end

f3=@(t,x) (t^3)/(x^2);
sol3=@(t) (3*t.^4/4+1).^(1/3);

Hh=zeros(6,1);% array to store the h used

for k=1:6
    h=0.1*2^(-k+1);
    [t,y]=e614(t0,T,h,f3,y0);
%     figure
%     t(end)
    err(k,3)=abs(y(end)-sol3(t(end)));
    Hh(k)=h;
end

err

loglog(Hh,err(:,1),Hh,err(:,2),Hh,err(:,3),Hh,Hh)
legend('error for first example','error for second example','error for third example','order 1 line','Location','best')
xlabel('h')
ylabel('error')
title('Convergence for 3 examples and line for order 1')
figure

loglog(Hh,Hh,Hh,Hh.^2,Hh,Hh.^3,Hh,Hh.^4,Hh.^5)
legend('order 1','order 2','order 3','order 4','order 5')
title('Different orders of convergence are straight lines in a log-log plot')
xlabel('h')
ylabel('h^p , p=order')
