%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Data to be interpolated declared here.
% First column of X are x values. Second
% column of X are y values. Below are two
% separate data sets:
%
clear all
x1=[1,2,3,4];
y1=[2,4,5,9];
x2=[1,2,3,3.5,4];
y2=[2,4,5,-2,9];
X1=[x1',y1'];
X2=[x2',y2'];
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Interpolating function is defined by
% phi(x,i) where i=1..n and
%
%   y=c(1)phi(x,1)+c(2)phi(x,2)+ ...c(n)phi(x,n)
%
%   is equivalent to the linear system
%
%           A c = Y
%
%   Form matrix Ai for data sets i=1,2
%
n1=length(x1);
for i=1:n1
    A1(:,i)=phi(X1(:,1),i-1);
end
%
n2=length(x2);
for i=1:n2
    A2(:,i)=phi(X2(:,1),i-1);
end
%
% Solve Ac=Y next and note its condition number.
%
Y1=X1(:,2);
c1=A1\Y1;
condA1=cond(A1);
%
%
Y2=X2(:,2);
c2=A2\Y2;
condA2=cond(A2);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% plot the data set 1
%
figure(1)
plot(x1,y1,'ro')
xlim([0,5])
ylim([-3,10])
xlabel('x')
ylabel('y')
title('Interpolating polynomial')
%
% layer the interpolating function over it
%
hold on
xx=[0:0.1:5];
yy=0*xx;
for i=1:n1
    yy=yy+c1(i)*phi(xx,i-1);
end;
plot(xx,yy,'b-')
hold off
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% plot the data set 2
%
figure(2)
plot(x2,y2,'ro')
xlim([0,5])
ylim([-3,10])
xlabel('x')
ylabel('y')
title('Interpolating polynomial')
%
% layer the interpolating function over it
%
hold on
xx=[0:0.1:5];
yy=0*xx;
for i=1:n2
    yy=yy+c2(i)*phi(xx,i-1);
end;
plot(xx,yy,'b-')
hold off
%
%

