
% Definicao de dominio
n = 4;
d = 1;
h = 2^(-n);
t = 0:h:1;

% Definicao do problema
c = 1;
Exata = @(t) exp(-c.*t);
F = @(t,x) -c.*x;
DF = @(t,x) -c;

Tol = 1e-6;
Erro = 1;

x = zeros(length(t),d);
x(1) = 1;
dx = 0;

for i = 2:length(t)

    f = @(y) x(i-1) + (h/2).*(F(t(i-1),x(i-1)) + F(t(i),y)) - y;
    Df = @(y) (h/2).*DF(t(i),y) - diag(ones(d,1));
    
    y0 = x(i-1);
    dy = 1;
    
    while abs(dy) > Tol
    
        dy = Df(y0)\(-f(y0));
        y0 = y0 + dy;
    end
        
    x(i) = y0;
    
end

figure;
plot(t,x,'*',t,Exata(t))

figure;
plot(abs(x - Exata(t)'))
title('Erro')
