Is there any way to do LFIT (general linear least-squares fit ) on the time series data using MATLAB?
3 views (last 30 days)
Show older comments
How to do the attched linear least-square fitting equation on time series data in matlab?
y = a + b*t + c*t^2 + d*sin(2*pi*t) + e*cos(2*pi*t) + f*sin(2*pi*t*2) + g*cos(2*pi*t*2)
a,b,c ... are parameters. t is time and y is dependent variable.
0 Comments
Answers (1)
Star Strider
on 27 Jul 2021
That depends entirely on what you want to do.
One approach:
syms a b c d e f g t
y = a + b*t + c*t^2 + d*sin(2*pi*t) + e*cos(2*pi*t) + f*sin(2*pi*t*2) + g*cos(2*pi*t*2);
yfcn = matlabFunction(y, 'Vars',{[a b c d e f g],t})
This will work in the Optimization Toolbox and Statictics and Machine Learning Toolbox nonlinear parameter estimation funcitons. Specify the initial parameter estimates as a row vector.
tv = linspace(0, 10, 50); % Create Data
yv = exp(-(tv-5).^2) + randn(size(tv))/10; % Create Data
B0 = rand(1,7); % Choose Appropriate Initial Parameter Estimates
B = nlinfit(tv(:), yv(:), yfcn, B0)
The Curve Fitting Toolbox does it differently:
yfit = fittype('a + b*t + c*t^2 + d*sin(2*pi*t) + e*cos(2*pi*t) + f*sin(2*pi*t*2) + g*cos(2*pi*t*2)', 'independent',{'t'});
yfit = fit(tv(:) ,yv(:), yfit,'start',B0)
Experiment to get the result you want.
.
6 Comments
Star Strider
on 28 Jul 2021
I still do not understand what you want to do.
If you want to filter the data, use the lowpass (or bandpass, to eliminate the baseline offset and drift) functions. I have no idea what you want to do, however
[yr_filt,lpdf] = lowpass(yr, 4E-3, Fs, 'ImpulseResponse','iir');
will work for the lowpass filter, and
[yr_filt,bpdf] = bandpass(yr,[1E-5, 4E-3], Fs, 'ImpulseResponse','iir');
for the bandpass design (although it might be necessary to experiment with the lower passband edge).
.
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!