function Noisy()
% Fitting noisy data with splines and piecewise cubic hermites

% Display the  function...
xVals = linspace(0,10,200)';
yVals = MyF(xVals);
plot(xVals,yVals,[0,10],[0,0],'r--')
title('f(x) = exp(-x/3)cos(2x) + .05*sin(10x)','fontsize',18)
h0 = xlabel('f(x) = smooth function + noisy function','fontsize',14);
shg
pause
% Show the points of interpolation...
hold
n = 20;
x = linspace(0,10,n)';
[y,yp] = MyF(x);
plot(x,y,'.k','markersize',20)
delete(h0)
h0 = xlabel('The points of interpolation','fontsize',14);
pause

% Interpolate with a spline...
S = spline(x,y);
sVals = ppval(S,xVals);
hold on
h=plot(xVals,sVals,'m','linewidth',3);
delete(h0)
h0 = xlabel('The spline interpolant','fontsize',14);
pause

% Interpolate with a piecewise cubic hermite..
[a,b,c,d] = pwC(x,y,yp);
cVals = pwCeval(a,b,c,d,x,xVals);
delete(h)
plot(xVals,cVals,'g','linewidth',3)
delete(h0)
h0 = xlabel('The piecewise cubic hermite interpolant','fontsize',14);

hold off



function [y,yp] = MyF(x)
delta = .05;
a = 10;
y = exp(-x/3).*cos(2*x) + delta*(sin(10*x)+cos(10*x));
yp = (-2*sin(2*x) - cos(2*x)/3).*exp(-x/3) + delta*a*(cos(a*x)-sin(a*x));