Matlab version of the polynomial interpolation program

(thanks to Shira)

Put the program in a file called polyint.m. Remove the 3 comments!! Run the program by defining 2 vectors, for example


X=[10 9 8 4 2 3];
Y=[0.1 0.3 0.4 0.9 0.8 0.5];

and calling polyint(X,Y). Here's the program:

function polyint(X,Y)

// find size of data, minimum and maximum x

n=size(X,2);
least=X(1);
most=X(1);
for i=2:n
 if X(i)<least
  least=X(i);
 end
 if X(i)$gt;most
  most=X(i);
 end
end

// make table of divided differences

table=zeros(n,n);
table(:,1)=Y';
for i=2:n
 for j=i:n
  table(j,i)=(table(j,i-1)-table(j-1,i-1))/(X(j)-X(j-i+1));
 end
end

// make graph of polynomial using Horner's method and plot

clf;
hold on;
x=[0:100]*(most-least)/100+least;
y=table(n,n);
for i=1:(n-1)
 y=y.*(x-X(n-i))+table(n-i,n-i); 
end
plot(X,Y,'*');
plot(x,y,'-.');

Go back to the page with the scilab program to see sample runs.