Exploring Interpolation

% Interpolation examples

% adapted by Yuri Lvov

% from Prof. Schwendeman webpage with some modifications

%

% Choose Number of points to play with:

Number=10;

t=linspace(.2,2.3,Number)

y=1./t+t.$ \hat{ }$2

a=polyfit(t,y,Number-1)

x=linspace(.2,2.3,100);

f=1./x+x.$ \hat{ }$ 2;

ftilde=polyval(a,x);

plot(x,f,'r',x,ftilde,'g',t,y,'mo')

% Shows Function, interpolation and dots on the same plot

% Try unequal spaced points, result is strange

t=[.2 .3 .5 .6 1.6 2.3]

y=1./t+t.$ \hat{ }$ 2

a=polyfit(t,y,Number-1)

x=linspace(.2,2.3,100);

f=1./x+x.$ \hat{ }$ 2;

ftilde=polyval(a,x);

plot(x,f,'r',x,ftilde,'g',t,y,'mo')

Number=20;

% This is the classical example producing wiggles

t=linspace(-1,1,Number);

% t=-cos((2*(1:Number)-1)*pi./(2*Number));

% Uncomment the preveous line for C. points

y=1./ (1+25* t.$ \hat{ }$ 2);

a=polyfit(t,y,Number-1);

x=linspace(-1,1,200);

f=1./(1+25*x.$ \hat{ }$ 2);

ftilde=polyval(a,x);

plot(x,f,'r',x,ftilde,'g',t,y,'mo')

% Do the same with SPLINE interpolation

% Type 'help spline' to get more info

% type 'help interp1' to get even more information

x = -1:.1:1;

y = 1./(1+25*x.$ \hat{ }$ 2);

xx = -1:.01:1;

yy = spline(x,y,xx);

plot(x,y,'o',xx,yy)