%% Smooth Data Using loess and rloess
% Plot and compare the results of data smoothed using the loess and rloess methods. Then determine which method is less sensitive to outliers.
% Create noisy data with two outliers.

x = (0:0.1:15)';
y = sin(x) + 0.5*(rand(size(x))-0.5);
y([90,110]) = 3;

%
% Smooth the data with the loess and rloess methods. Use a span of 10% of the total number of data points.

yy1 = smooth(x,y,0.1,'loess');
yy2 = smooth(x,y,0.1,'rloess');

%
% Plot the original and smoothed data. The outliers have less influence with the robust method rloess.

subplot(2,1,1)
plot(x,y,'b.',x,yy1,'r-')
set(gca,'YLim',[-1.5 3.5])
legend('Original data','Smoothed data using ''loess''',...
       'Location','NW')
   
subplot(2,1,2)
plot(x,y,'b.',x,yy2,'r-')
set(gca,'YLim',[-1.5 3.5])
legend('Original data','Smoothed data using ''rloess''',...
       'Location','NW')

% Copyright 2012 The MathWorks, Inc.