clear

N = 10; % polynomial degree

% f = @(x) sin(2*pi*x);
a = 25;
f = @(x) 1./(1+a*(x-.5).^2);

% projection using monomials
M = zeros(N+1);
for i = 0:N
    for j = 0:N
        M(i+1,j+1) = 1/(i+j+1);
    end
end
b = zeros(N+1,1);
for i = 0:N
    b(i+1) = integral(@(x) x.^i .* f(x),0,1);
end
cm = M\b;

% estimate roundoff error 
e = ones(N+1,1);
b = M*e;
fprintf('roundoff in reproducing e: %g\n',max(abs( M \ b - e)))

% plot result
x = linspace(0,1,500);
hold on
plot(x,f(x),'-')

u = 0; ui = 0;
for i = 0:N
    u = u + cm(i+1)*x.^i;
    ui = ui + ci(i+1)*x.^i;
end

% plot(x,ui,'-.')
plot(x,u,'--')

% legend('Exact','Interpolant','L2 projection with monomials')