% Example3_49 Computations for Example 3.49.
%
fprintf('\n')
disp('                  With Fourier_Gauss coefficients')
%disp('                  With Fourier_Lobatto coefficients')
fprintf('\n')
f1='%15.0f %12.3e %12.3e\n';
disp('              n       E2          Einf')
N=10; k=(1:N)'; E2=zeros(N,1); Einf=zeros(N,1);
xw(k,1)=cos((2*k-1)*pi/(2*N)); xw(k,2)=pi/N;
%xw(k,1)=cos((N-k)*pi/(N-1)); xw(k,2)=pi/(N-1);
%xw(1,2)=.5*pi/(N-1); xw(N,2)=.5*pi/(N-1);
f=exp(-xw(:,1));
%f=log(2+xw(:,1));
%f=sqrt(1+xw(:,1));
%f=abs(xw(:,1));
c=fourier_gauss(N,f);
%c=fourier_lobatto(N,f);
%c(N)=.5*c(N);
for n=1:N
  E2(n)=sqrt(sum(xw(:,2).*((clenshaw_cheb(n-1,xw(:,1),c)-f).^2)));
end
t=linspace(-1,1)'; p=zeros(100,N);
ft=exp(-t); 
%ft=log(2+t);
%ft=sqrt(1+t);
%ft=abs(t);
for n=1:N
  p(:,n)=clenshaw_cheb(n-1,t,c);
  Einf(n)=max(abs(p(:,n)-ft));
  if n==1
    fprintf('%15.0f %12.3e %12.3e    f(t)=exp(-t)\n',n-1,E2(n),Einf(n))
%    fprintf('%15.0f %12.3e %12.3e    f(t)=log(2+t)\n',n-1,E2(n),Einf(n))
%    fprintf('%15.0f %12.3e %12.3e    f(t)=sqrt(1+t)\n',n-1,E2(n),Einf(n))
%    fprintf('%15.0f %12.3e %12.3e    f(t)=abs(t)\n',n-1,E2(n),Einf(n))
  else
    fprintf(f1,n-1,E2(n),Einf(n))
  end
end
