Clear Filters
Clear Filters

2D data fitting - Surface

10 views (last 30 days)
Patrick
Patrick on 12 Oct 2018
Commented: Patrick on 12 Oct 2018
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 Comment
Patrick
Patrick on 12 Oct 2018
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

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!