%%
% generate some data points (X(j),Y(j))
X=0:0.2:1;
Y=[1 1.1 1.3 0.8 0.4 1.0];

% display the data we generated
figure
plot(X,Y,'o','MarkerSize',10,'LineWidth',2)
hold on

% adjust the axes for better display (type "help axis" to learn how to use
% the "axis" command
axis([-0.1, 1.1, 0, 1.5])

% draw some grid lines
grid

% set n to the length (ambient dimension) of x
n=length(X)

V=vander(X);

a=V\Y'

x=linspace(0,1,100)

y=polyval(a,x)

plot(x,y,'LineWidth',2)

%%
%%%%
% Repeat after perturbing Y slightly
X=0:0.2:1;
Y1=[1 1.1 1.3 0.8 0.4 1.0];
Y1(2)=Y1(2)+0.02;

plot(X,Y1,'mx','MarkerSize',10,'LineWidth',2)
hold on

%axis([-0.1, 1.1, 0, 1.5])

%n=length(X)

%V=vander(X);

a1=V\Y1'

%x=linspace(0,1,100)

y1=polyval(a1,x)


plot(x,y1,'k','LineWidth',2);

%%

%%% With more points
figure

X=linspace(0,1,25);
Y=sin(2*pi*X);
plot(X,Y,'o','MarkerSize',10,'LineWidth',2)
axis([-0.1 1.1 -1.5 1.5])
hold on
V=vander(X);
a=V\Y';
XL=linspace(0,1,500);
YL=polyval(a,XL);
plot(XL,YL,'LineWidth',2);

Y1=Y;
Y1(5)=Y1(5)+0.01;
Y=Y1;
plot(X,Y,'o','MarkerSize',10,'LineWidth',2)
axis([-0.1 1.1 -1.5 1.5])
hold on
V=vander(X);
a=V\Y';
XL=linspace(0,1,500);
YL=polyval(a,XL);
plot(XL,YL,'k','LineWidth',2);

%%

%%% With more perturbations
figure

X=linspace(0,1,30);
Y=sin(2*pi*X);
plot(X,Y,'o','MarkerSize',10,'LineWidth',2)
axis([-0.1 1.1 -1.5 1.5])
hold on
V=vander(X);
a=V\Y';
XL=linspace(0,1,500);
YL=polyval(a,XL);
plot(XL,YL,'LineWidth',2);

Y2=Y;
Y2=Y2+0.01*rand(size(Y2));
Y=Y2;
plot(X,Y,'x','MarkerSize',10,'LineWidth',2)
axis([-0.1 1.1 -1.5 1.5])
hold on
V=vander(X);
a=V\Y';
XL=linspace(0,1,500);
YL=polyval(a,XL);
plot(XL,YL,'k','LineWidth',2);


%%
figure

X=linspace(0,1,25);
Y=4*X;
plot(X,Y,'o','MarkerSize',10,'LineWidth',2)
axis([-0.1 1.1 -1.5 4.5])
hold on
V=vander(X);
a=V\Y';
XL=linspace(0,1,500);
YL=polyval(a,XL);
plot(XL,YL,'LineWidth',2);

Y1=Y;
Y1(5)=Y1(5)+0.01;
Y=Y1;
plot(X,Y,'x','MarkerSize',10,'LineWidth',2)
axis([-0.1 1.1 -1.5 1.5])
hold on
V=vander(X);
a=V\Y';
XL=linspace(0,1,500);
YL=polyval(a,XL);
plot(XL,YL,'k','LineWidth',2);
