clear all
close all
% esercizio 1 PDE, differenze finite, esponenziale-punto medio
mrange = 10:10:50;
counter = 0;
a = 0;
b = pi/2;
tstar = 1;
ts = 50000;
k = tstar/ts;
for m = mrange+1
    counter = counter+1;
    x = linspace(a,b,m)';
    u0 = sin(x);
    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;
    P = phi1m(k*A);
    y = u0;
    t = 0;
    for n = 1:ts
        y = y+k*P*(A*y+[0;2*exp(t+k/2)*sin(x(2:m))]);
        t = t+k;
    end
    errs(counter) = norm(y-exp(tstar)*sin(x),inf);
end
loglog(mrange,errs,'*',mrange,mrange.^(-2))
m = 200;
x = linspace(a,b,m)';
u0 = sin(x);
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;
tsrange = 10:10:50;
counter = 0;
for ts = tsrange
    counter = counter+1;
    k = tstar/ts;
    P = phi1m(k*A);
    y = u0;
    t = 0;
    for n = 1:ts
        y = y+k*P*(A*y+[0;2*exp(t+k/2)*sin(x(2:m))]);
        t = t+k;
    end
    errt(counter) = norm(y-exp(tstar)*sin(x),inf);
end
figure
loglog(tsrange,errt,'*',tsrange,tsrange.^(-2))