%% Compare Two Cubic Smoothing Splines Obtained from Noisy Data
% This code returns a fit to the noisy data that is expected to be quite close to the underlying noisefree data since the latter come from a slowly varying function and since the used TOL is of the size appropriate for the size of the noise.

x = linspace(0,2*pi,21); y = sin(x) + (rand(1,21)-.5)*.2;
sp = spaps(x,y, (.05)^2*(x(end)-x(1)) );
fnplt(sp)

% This code uses the same data and tolerance as before, but chooses the roughness weight to be only 0.1 in the right half of the interval and gives, correspondingly, a rougher but better fit there.

sp1 = spaps(x,y, [(.025)^2*(x(end)-x(1)),ones(1,10),repmat(.1,1,10)] );
fnplt(sp1)

% Finally, compare the two cubic smoothing splines obtained previously.

fnplt(sp); 
hold on
fnplt(sp1,'r')
plot(x,y,'ok')
hold off
title('Two cubic smoothing splines')
xlabel('The red one has reduced smoothness requirement in right half.')

% Copyright 2019 The MathWorks, Inc.