Nonlinear data-fitting
显示 更早的评论
I have the data below, where y=variables(:,2), x=variables(:,1) and t=length(x).
Do you have any idea how to get e better fitting in mdl2?
myfun=@(b,x) b(1)+b(2)*t+b(3)*t.^2;
InitGuess=[8.2075e+05 1 1];
mdl1=fitnlm(t,y,myfun,InitGuess);
YY=feval(mdl1,t);
figure,plot(t,1-YY./y'-b',t,0*t,'-r'), title('Relativ error ')
figure, plot(t,y,'-g',t,YY,'-b'), title('Data and model'), legend('Data','Model')
%%
Z=y-YY;
figure,plot(Z)
X=[t x];
f1=@(a,X) sin(a*X);
f2=@(a,X) cos(a*X);
myfun=@(b,X) b(1)*f1(2*pi/30.5,X(:,1)).*X(:,2) + b(2)*f2(2*pi/31.5,X(:,1)) + ...
+ b(3)*f2(pi/2.3,X(:,1))+ b(4)*f1(pi/2.3,X(:,2)).*X(:,1)+b(5)*X(:,2).*X(:,1) +b(6)*X(:,2)+b(7)*X(:,1);
InitGuess=[-7.6342e-08 1 1 1 1 1 1];
mdl2=fitnlm(X,Z,myfun,InitGuess)
ZZ=feval(mdl2,X);
2 个评论
Walter Roberson
2020-1-29
myfun=@(b,X) b(1)*f1(2*pi/30.5,X(:,1)).*X(:,2) + b(2)*f2(2*pi/31.5,X(:,1)) + ...
+ b(3)*f2(pi/31.5,X(:,1))+ b(4)*f1(pi/2.3,X(:,2)).*X(:,1)+b(5)*X(:,2).*X(:,1) +b(6)*X(:,2)+b(7)*X(:,1);
Has subexpression
b(2)*f2(2*pi/31.5,X(:,1)) + ...
+ b(3)*f2(pi/31.5,X(:,1))
The f2 parts are the same so that is (b(2)+b(3)) times the f2 part. You would then combine b(2)+b(3) in to a single parameter.
Or is there a mistake in the formula?
采纳的回答
更多回答(1 个)
Hiro Yoshino
2020-1-29
0 个投票
I just wonder if this is a linear model?
(1) Is b a coefficient vector?
(2) Do you need to estimate "a" too?
if (1) yes, (2) no, then this is a linear model and you can solve this analytically.
4 个评论
Walter Roberson
2020-1-29
The first section of it looks like it should just be polyfit with degree 2.
gjashta
2020-1-29
Hiro Yoshino
2020-1-30
sorry for late.
Well, these are linear models, i.e., you don't need to run optimization to obtain parameters.
The solutions are analytically calculated.

As long as the parameters $$\mathbf{b}$$ are linear with respect to the given data $$X$$, the problem is called "linear problem". The solution can be given by
. in matlab, fitlm is the one you should apply to this problem.
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!