% plotInterpPoly.m
% script from AG p.92, with some modifications and comments
t = [0 , 0.1 , 0.8 , 1]’; % column vector
b = [ 1 , -0.9 , 10 , 9 ]’; % column vector of data values
Copyright By PowCoder代写 加微信 powcoder
A = zeros(4,n);
powers = 0 : n-1;
% set up the Vandermonde matrix
for j=1:n % set the columns of A one at a time
A(:,j) = t.^powers(j); % componentwise exponentiation of vector t
% get the coefficients of the interpolating polynomial
x = A \ b ; % Matlab’s backslash operator solves Ax=b
tt = (-0.1 : .01 : 1.1)’; % for plotting purposes
% evaluate the polynomial on tt
% in general, it would be better to use Horner’s Rule on AG p.11 or
% call Matlab’s polyval, but this is simple and correct
pt = x(1) + x(2)* tt + x(3)* tt.^2 + x(4)*tt.^3;
clf % clear current figure
% plot the polynomial on tt
plot(tt,pt)
% plot the original data as well
plot(t, b, ‘ro’)
xlabel(‘t’)
ylabel(‘v’)
title(‘Cubic polynomial interpolating 4 given data points’)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com