% Fit a combination of e^x, sin(x), gamma(x) to the function f(x)=1/x. % Tref&Bau problem 11.2(a). ELB 11/8/03 m=400; % number of subintervals in discretization. x=linspace(1,2,m+1)'; A=[exp(x) sin(x) gamma(x)]; b=1./x; disp('coefficients of approximation [of exp(x) sin(x) gamma(x), respectively]:') c=A\b; format long g, format compact, disp(c') err=A*c-1./x; errl=err(1:m); % errl has left endpt values format short e, disp('estimated L^1, L^2, and L^infty error:') disp([sum(abs(err))/m sqrt(sum(err.^2)/m) max(abs(err))]) figure(1), clf, plot(x,A*c,x,1./x), axis([1 2 .5 1]) title('f(x)=1/x superimposed on linear combination of exp(x), sin(x), gamma(x)') figure(2), clf, plot(x,err), grid on title('signed error in approximation')