clear all
close all
a = 0;
b = pi/2;
m = 150;
x = linspace(a,b,m)';
h = (b-a)/(m-1);
A = toeplitz(sparse([1,1],[1,2],[-2,1]/h^2,1,m));
A(1,1:2) = [0,0];
A(m,m-1:m) = [2,-2]/h^2;
tstar = 1;
u0 = sin(x);
tsrange = 10:10:50;
counter = 0;
for ts = tsrange
    counter = counter+1;
    k = tstar/ts;   
    P = phi1m(k*A);
    u = u0;
    t = 0;
    for n = 1:ts
        u = u+k*P*(A*u+2*[0;exp(t+k/2)*sin(x(2:m))]);
        t = t+k;
    end
    erroret(counter) = norm(u-exp(tstar)*sin(x),inf);
end
loglog(tsrange,erroret,'*',tsrange,tsrange.^(-2))
xlabel('ts')
title('errore temporale')
mrange = 50:50:250;
ts = 1000;
k = tstar/ts;
counter = 0;
for m = mrange+1
    counter = counter+1;
    x = linspace(a,b,m)';
    h = (b-a)/(m-1);
    A = toeplitz(sparse([1,1],[1,2],[-2,1]/h^2,1,m));
    A(1,1:2) = [0,0];
    A(m,m-1:m) = [2,-2]/h^2;
    u0 = sin(x); 
    t = 0;
    P = phi1m(k*A); 
    u = u0;
    for n = 1:ts
        u = u+k*P*(A*u+2*[0;exp(t+k/2)*sin(x(2:m))]);
        t = t+k;
    end
    errores(counter) = norm(u-exp(tstar)*sin(x),inf);
end
figure
loglog(mrange,errores,'*',mrange,mrange.^(-2))
xlabel('m')
title('errore spaziale')
