Non linear fit extremely bad : what am I doing wrong ?
9 次查看(过去 30 天)
显示 更早的评论
Hello everybody,
I've spent my evening trying to understand why the nonlinear fit was so bad with my data and I couldn't find why.
I tried many different options, like the 'fit' function, 'fminsearsh', the fitting curve tool, etc...
For some reasons, the fit is OK only if my starting points are close from the real coeff at 0.1% ...
What am I doing wrong ? 1.1 isn't THAT far from 1. (same problem without the constant 'a')
Thank you for your help,
close all
clear all
rng('default')
t = 0:0.02:10; x = t.*sin(2*pi*1*t) + 0.1*randn(1, length(t)); % x*sin(x) with noise
x = x'; t = t';
% fitting part
x0 = [0 1.1];
fitfun = fittype( @(a,b,x) a+x.*sin(2*pi*b*x) );
[fitted_curve,gof] = fit(t,x,fitfun,'StartPoint',x0)
0 个评论
采纳的回答
Matt J
2024-3-14
编辑:Matt J
2024-3-15
There are ways to derive an accurate x0 more systematically, e.g.,
rng('default')
t = 0:0.02:10; x = t.*sin(2*pi*1*t) + 0.1*randn(1, length(t)); % x*sin(x) with noise
x = x'; t = t';
%Coarse fit
a0=mean(x);
I=t>=1;
z=(x(I)-a0)./t(I); %Ideally, z would be purely sinusoidal
cfit=fit(t(I),z,'fourier1');
% Fine fit
x0 = [a0 cfit.w/2/pi];
fitfun = fittype( @(a,b,x) a+x.*sin(2*pi*b*x) );
[cfit,gof] = fit(t,x,fitfun,'StartPoint',x0),
plot(cfit,t,x)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!