clc, clear all;

n=4;

for i=1:4
h=1/n;

x=(1+h:h:2-h)';
sup=[0;1/h^2-(2-2*x(1:end-1)+x(1:end-1).^2)/(2*h)];
sub=[1/h^2+(2-2*x(2:end)+x(2:end).^2)/(2*h);0];
dia=ones(size(x))-(2/h^2);
A=spdiags([sub dia sup],-1:1,n-1,n-1);
b=zeros(size(x));
b(end)= -(1/h^2-(2-2*x(end)+x(end)^2)/(2*h));
y=A\b;
plot([1; x;2],[0;y;1])
hold on
if i>1
    integralengammal=integralen;
end
integralen= (sum(y)+1/2)*h
if i >1
    skillnaden=integralen - integralengammal
end
n=n*2;
end