% Use Chebfun to find the best approximation to exp(x) from span{x}

 v = chebfun('exp(x)',[0 1]);    % function we want to approximate
 phi = chebfun('x',[0 1]);       % basis vector for the approximating subspace

 w_hat = ((phi'*v)/norm(phi)^2)*phi;    % formula for the best approximation


% plotting commands (with fancy LaTeX legends)

 figure(1), clf
 plot(v,'b-','linewidth',2)
 hold on
 plot(phi,'r-','linewidth',2)
 xlabel('$x$','fontsize',20,'interpreter','latex')
 leg=legend('$v$','$\phi$','location','northoutside','orientation','horizontal');
 set(leg,'fontsize',20,'interpreter','latex')
 input('hit return to see best approximation')
 plot(w_hat','m-','linewidth',2)
 leg=legend('$v$','$\phi$','$\hat{w}$','location','northoutside','orientation','horizontal');
 set(leg,'fontsize',20,'interpreter','latex')

