%This file is free for public use. However, the comment lines should not
%be removed.
%--------------------
%I am the script file inter.m
%I was created in the fall of 1999, as a part of a class demonstration
%of polynomial interpolation to cs412 students. 
%I was written by Amos Ron.
%Note that I am  intentionally deficient, since I use the Vandermonde
%determinant approach.
%--------------------
%this file will ask you to input `x' and `F'
% be aware that running the file will overwrite your variables
%V, j, n, Z, p, a
%if you want to avoid it, convert this file from a script file to
%a function file. See the matlab primer.
%--------------------
%we are trying to fit a polynomial to a given function
%symbols used here are as follows:
%`x' is the vector of length n where the interpolation points are stored
%`F' is a vector where the functions values of the function F at the
%interpolation point are stored. `a' is the vector where the coefficients
%of the polynomial interpolant are stored.
%`z' is a dense mesh of the interval of consideration

colr='grkxgrkwgrbkwgrbkw';
i=i+1;
V=0; j=0;
clear V, clear j
x=input('Enter the vector of interpolation points:');
F=input('Enter the vector of function values at the interpolation points:');
n=length(x); 

%the next line computes an interval that contains the interpolations points
%xx=sort(x); z=linspace(xx(1),xx(n),60);

%now, we generate the vandermonde's matrix at the interpolation points
%it can be done with simple
%loops.  however, it can also be done
%without loops. you need to know one additional command: 
%let us assume that we have a column vector x and we would like to generate a
% matrix X with m columns, each of which being identical to x
% the command is
%X=x(:,ones(1,m));
J=[n-1:-1:0]';
V=zeros(n);

V=x(:,ones(1,n)).^(J(:,ones(1,n))');
%`V' is the vandermonde matrix
a=V\F;
%this puts in `a' the solution of the equation `Va=F'

p=polyval(a,z);
%plot(z,p,colr(i))
%the matlab m-file `polyval.m' evaluates the polynomial whose coefficients
%are stored at `a', at each point of the vector x, i.e.,
%p=a(1)*x^(n-1)+a(2)*x^(n-2)+.... a(n)*x^0.
