I am trying to fit for the first time in Matlab, a two dimensional datapoints [x, y]
(both x
and y
are 1D entries) using the locally weighed regression algorithm (LOWESS). I implemented the code snippet below which uses a noisy Gaussian curve and try to recover [x, yfitted]
using the LOWESS method.
clearvars;
close all;
clc;
n = 10;
fit_frac = 0.2;
x = zeros(n, 1); x(1:end) = 1:n;
dx = 0.1;
x = x*dx;
xcenter = (n + 1)/2*dx;
x = x - xcenter;
% Gaussian initial signal
y = exp(-x.^2./sqrt(2));
% ad an offset of 1%;
y = y + 0.01.*max(y).*(rand(n, 1)*2 - 1);
%
datain = [x, y];
argfrac = fit_frac.*ones(n, 1);
% f = fit(data(cid).w_store.', data(cid).p_store.', 'lowess');
f = fit(datain, argfrac, 'lowess');
Nevertheless, when I
figure; plot(f);
draw a 3D plot instead of a 2D plot of the fitted data which in my best scenario will recover the original Gaussian distribution.
Can anyone help me fix this?
Your syntax, fit([x,y],z,...)
fits a surface. And the lowess
model is only supported for fitting surfaces (as per the documentation). So, you will have a fitted surface in f
.
You want to fit a Gaussian, so you should use the gauss1
model for fitting:
f = fit(x, y, 'gauss1');
plot(f, x, y)