
function demo_ls(exampleV)

warning off

if nargin < 1
    exampleV=1:3;
end

m=[1:20];




for jj=1:length(exampleV)
    example=exampleV(jj);
    [f,a,b]=choose_example(example);
    xt=linspace(a,b,10001);
    yt=feval(f,xt);
    for ii=1:length(m)
        mL=m(ii);
        nL=2*mL^2+1;
        x=linspace(a,b,nL);
        y=feval(f,x);
        Lm = polyfit(x,y,mL);
        Lmxt=polyval(Lm,xt);
        err(ii,jj)=norm(Lmxt-yt,inf);
        lengths(ii,jj)=length(x);
    end
end

for ii=1:length(m)
    fprintf('\n \t $%3.0f$',m(ii));
    for jj=1:length(exampleV)
        fprintf('& $%1.1e$ ',err(ii,jj));
    end
    fprintf(' & $%10.0f$ \\\\',lengths(ii,end));
end
fprintf('\\\\');


fprintf('\n \t')






function [f,a,b]=choose_example(example)
switch example
    case 1
        f=@(x) x.^30;
        a=0;
        b=1;
    case 2
        f=@(x) x.^(7/2);
        a=0;
        b=1;
    case 3
        f=@(x) exp(x);
        a=0;
        b=1;
    case 4
        f=@(x) 1./(1+x.^2);
        a=-5;
        b=5;
end