필터 지우기
필터 지우기

2D data fitting - Surface

조회 수: 10 (최근 30일)
Patrick
Patrick 2018년 10월 12일
댓글: Patrick 2018년 10월 12일
Dear all,
I wanted to adapt the post 2D data fitting - Surface that uses lsqcurvefit to fit data defined on a 2D grid and use instead nlinfit and fitnlm.
For nlinfit replacing the following
B = lsqcurvefit(surfit, [0.5 -0.5 -0.5], XY, z, [0 -10 -10], [1 10 10])
with
flatten = @(X) X(:);
Surfit = @(B,XY) flatten(surfit(B,XY));
B = nlinfit(XY,flatten(z),Surfit,[0.5 -0.5 -0.5],statset('Display','final'))
seems to works fine even though results differ slightly but I can't figure out how to do the same with fitnlm. I tried
flatten = @(X) X(:);
Surfit = @(B,XY) flatten(surfit(B,XY));
B = fitnlm(XY,flatten(z),Surfit,[0.5 -0.5 -0.5],'Options',statset('Display','final','Robust','On'))
but I get the following error
Error using classreg.regr.FitObject/assignData (line 140)
All predictor and response variables must be vectors or matrices.
Error in NonLinearModel.fit (line 1417)
model =
assignData(model,X,y,weights,[],model.Formula.VariableNames,exclude);
Error in fitnlm (line 99)
model = NonLinearModel.fit(X,varargin{:});
Error in fit2d (line 69)
B = fitnlm(XY,flatten(z),Surfit,[0.5 -0.5 -0.5],...
Any suggestion on how to proceed most welcome!
Many thanks, Patrick
  댓글 수: 1
Patrick
Patrick 2018년 10월 12일
I think I found a solution using a table
tbl = table(flatten(XY(:,:,1)),flatten(XY(:,:,2)),flatten(z));
Surfit = @(B,XY) B(1)*exp(B(2).*XY(:,1)) + (1 - exp(B(3).*XY(:,2)));
nlm = fitnlm(tbl,Surfit,[0.5 -0.5 -0.5],'Options',statset('Display','final','Robust','On'));
nlm.Coefficients{:,1}'
But they seem to provide quite different answers with these data as seen in the output of the updated code attached
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the default value of the function tolerance.
B =
0.6343 -0.4053 -0.2029
Iterations terminated: relative change in SSE less than OPTIONS.TolFun
B =
0.6340 -0.4044 -0.2026
Iterations terminated: relative norm of the current step is less than OPTIONS.TolX
ans =
0.1564 -0.2730 -0.2366
Any comment on the discrepancies welcome.
Many thanks,
Patrick

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile 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!

Translated by