%% Fit Splines with Different Smoothing Parameters
% Fit smoothing splines using the csaps function with different values for the smoothing parameter p. Use values of p between the extremes of 0 and 1 to see how they affect the shape and closeness of the fitted spline.
% Load the titanium data set.

[x, y] = titanium();

% When p = 0, s0 is the least-squares straight line fit to the data. When p = 1, s1 is the variational, or natural, cubic spline interpolant.
% For 0 < p < 1, sp is a smoothing spline that is a trade-off between the two extremes: smoother than the interpolant s1 and closer to the data than the straight line s0.

p = 0.00009;

s0 = csaps(x,y,0);
sp = csaps(x,y,p);
s1 = csaps(x,y,1);

%

figure
fnplt(s0);
hold on
fnplt(sp);
fnplt(s1);
plot(x,y,'ko');
hold off
title('Smoothing splines with different values for p');
legend('p = 0', ['p = ' num2str( p )], 'p = 1', 'Location', 'northwest')

%
% Copyright 2018 The MathWorks, Inc.
%