% fn.m function y = fn(x) % on input x is independent variable % return function values for function to be interpolated %y = sin(2*pi*x); %y = exp(-10.*(x-.5).^2); n = length(x); y = rand(n,1); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % interpV.m function c = interpV(x,y) % input: % x: column vector with distinct entries % y: column vector of data % output: % c: column vector of interp. poly coefficients s.t. p(x(i)) = y(i) n = length(x); vandermonde c = v\y; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % poly.m % polynomial evaluation at a point z % on input: c = vector of coeffs of polynomial % nested multiplication n = length(c); pval = 0; for i=n:-1:1 pval = z*pval + c(i); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %polyTest.m %driver to illustrate polynomial interpolation % set up points to be interpolated from a known function x=0:.1:1; x=x'; y=fn(x); %set up matrix and get interpolating coefficients vandermonde c=interpV(x,y); %evaluate polynomial at many more points zx=0:.01:1; zx=zx'; vpoly plot(zx,pval,'c',x,y,'m',x,y,'m+'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % vandermonde.m % set up vandermonde matrix % on input, given x = column vector of interpolating points n = length(x); v = ones(n,n); for j=2:n v(:,j) = x.*v(:,j-1); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % vpoly.m % polynomial evaluation for entire vector zx % on input: c = vector of coeffs of polynomial % nested multiplication n = length(c); m = length(zx); pval = zeros(m,1); for i=n:-1:1 pval = zx.*pval + c(i); end