%
%  least L2 poly -- djm, 29 june 2005
%

if (exist('inp')==0)
    N = input('N => ');
end
    
c = zeros(N+1,1);  c(1) = exp(1)-1;
for j=2:N+1
    c(j) = exp(1) - (j-1)*c(j-1);
end

H = hilb(N+1);

a = H\c;

x = 0:0.01:1;
p = a(end)*ones(size(x));
for j = N:-1:1
    p = p.*x + a(j);  
end

figure(1);  clf;  hold on
subplot(2,1,1);  hold on
plot(x,exp(x),'r');
plot(x,p,'k--');
title(['\bf least L2 polynomial (N = ' num2str(N) ')'])
subplot(2,1,2);  hold on
error = exp(x)-p;  max_error = max(error)
plot(x,exp(x)-p,'r');
title(['\bf max |f(x)-Q_N(x)| = ' num2str(max_error)])



