f = @(x) 1./(1+x.^2) f = function_handle with value: @(x)1./(1+x.^2) x=linspace(-5,5,100); plot(x,f(x)); N=4; xs=linspace(-5,5,N+1);ys=f(xs); p = polyfit(xs,ys,N); plot(x,f(x),x,polyval(x,p),'LineWidth',1.5); Error using plot Vectors must be the same length. plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); N=6; xs=linspace(-5,5,N+1);ys=f(xs); p = polyfit(xs,ys,N); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); N=8; xs=linspace(-5,5,N+1);ys=f(xs); p = polyfit(xs,ys,N); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); N=10; xs=linspace(-5,5,N+1);ys=f(xs); p = polyfit(xs,ys,N); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); N=8; xs=linspace(-5,5,N+1);ys=f(xs); p = polyfit(xs,ys,N); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); N=10; xs=linspace(-5,5,N+1);ys=f(xs); p = polyfit(xs,ys,N); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); N=14; xs=linspace(-5,5,N+1);ys=f(xs); p = polyfit(xs,ys,N); Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT. In polyfit (line 72) plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); N=8; xs=chebpoints(-5,5,N);ys=f(xs); p = polyfit(xs,ys,N); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); N=12; xs=chebpoints(-5,5,N);ys=f(xs); p = polyfit(xs,ys,N); plot(x,f(x),x,polyval(p,x),'LineWidth',1.5); N=16; xs=chebpoints(-5,5,N);ys=f(xs); p = polyfit(xs,ys,N); Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT. > In polyfit (line 72) plot(x,f(x),x,polyval(p,x),'LineWidth',1.5);