f=inline('1./(1+12*x.^2)'); %% Example 1: Seven points %First define the points for the polynomial: xp=linspace(-1,1,7); yp=f(xp); %Find the coefficients using the Vandermonde matrix: V=vander(xp); C=V\yp'; xx=linspace(-1,1,200); %Pts for f, P yy1=f(xx); yy2=polyval(C,xx); plot(xp,yp,'r*',xx,yy1,xx,yy2); legend('Data','f(x)','Polynomial') title('Interpolation using 7 points, Degree 6 poly') %% Example 2: 15 Points %First define the points for the polynomial: xp=linspace(-1,1,15); yp=f(xp); %Find the coefficients using the Vandermonde matrix: V=vander(xp); C=V\yp'; xx=linspace(-1,1,200); %Pts for f, P yy1=f(xx); yy2=polyval(C,xx); figure(2) plot(xp,yp,'r*',xx,yy1,xx,yy2); title('Interpolation using 15 points, Degree 14 Poly') legend('Data','f(x)','Polynomial') %% Example 3: 30 Points, degree 9 %First define the points for the polynomial: xp=linspace(-1,1,30); yp=f(xp); %Find the coefficients using vander1.m (written by us) V=vander1(xp',10); C=V\yp'; xx=linspace(-1,1,200); %Pts for f, P yy1=f(xx); yy2=polyval(C,xx); figure(3) plot(xp,yp,'r*',xx,yy1,xx,yy2); title('Interpolation using 30 points, Degree 10 Poly') legend('Data','f(x)','Polynomial')