%% Sauer Computer Problen 6.1.3
close all
clear all
clc

F = @(t,Y) [t;...
    Y(2)*t*t;...
    2*(t+1)*Y(3);...
    5*Y(4)*t^4;...
    1/Y(5)^2;...
    (t^3)/(Y(6)^2)];

Y_ana = [1+1/2;...
    exp(1/3);...
    exp(3);...
    exp(1);...
    4^(1/3);...
    (3/4+1)^(1/3)];

t = 0;
errorV = zeros(6,6);
h_V = 0.1*2.^(-(0:5));
for k= 0:5
    Y = ones(6,1);
    t = 0;
    
    h = 0.1*2^(-k);
    n_steps = 1/h;
    
    for i = 1:n_steps
        Y = Y + h * F(t,Y);
        t = t  + h;
    end
    errorV(:,k+1) = abs(Y-Y_ana);
end
figure
hold on

for i = 1:6
    subplot(2,3,i)
    loglog(h_V,errorV(i,:))
    slope = mean((log10(errorV(i,2:end))-log10(errorV(i,1:(end-1))))./((log10(h_V(2:end))-log10(h_V(1:(end-1))))))
end
hold off


