%This file is free for public use. However, the comment lines should not
%be removed.
%--------------------
%I am the function file inter1.m
%I was created in the winter of 1997, 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.
%--------------------

function  [xval,yval]=inter1(x,F,lw)
%the input are the interpolation point x, and the data values F at x
%the output are xvalues and yvalues of the polynomial interpolant of
%the data.
%in addition to the output, the function will also plot the graph of the
%polynomial
%--------------------
%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

V=0; j=0;
clear V, clear j
n=length(x);
xx=sort(x);
xval=linspace(xx(1),xx(n),60);

%now, we generate the vandermonde's matrix at the interpolation points
%it can be done with simple
%loops (cf. the matlab book page 72).  however, it can also be done
%without loops. you need to know one addtionaal 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'

yval=polyval(a,xval);
if nargin<3, lw=1; end
plot(xval,yval,'linewidth',lw);
%the matlab m-file `polyval.m' evaluates the polynomial whose coefficients
%are stored at `a', at each point of the vector x, i.e.,
%yval=a(1)*x^(n-1)+a(2)*x^(n-2)+.... a(n)*x^0.
