MATLAB Answers

Problem with nonlinear curve fitting - lsqcurvefit

37 views (last 30 days)
Alex on 4 Mar 2013
Commented: Deniz Toprak on 26 Jul 2020
I'm trying to fit a nonlinear curve with constrains, so I use lsqcurvefit to get the parameters of my function. After applying lsqcurvefit I obtain this output:
Local minimum possible.
So I use MultiStart in order to get the global minimum, but it is not able to obtain the parameters of the function. In this case the output is:
MultiStart encountered failures in the user provided functions. All 100 local solver runs failed in a user supplied function
Searching on Matlab answers I came across this post
They suggest patternsearch in situations where an objective function has many local minima. However, it is not clear how to use pattern search to fit a nonlinear curve.
How can I obtain an optimal fitting? How can I use pattern search when fitting a nonlinear curve?
Any help will be appreciated. Thanks!

  1 Comment

Matt J
Matt J on 4 Mar 2013
It could be worthwhile seeing your code, your curve model, and how you are initializing the iterations. Sometimes, it takes some problem-specific artistry, to avoid naive initial guesses.

Sign in to comment.

Accepted Answer

Shashank Prasanna
Shashank Prasanna on 4 Mar 2013
Hi Alex, this link may help you get started:
Using optimization routines in MATLAB are very similar across functions. The idea in fitting a curve is to set up an error function usually sum of squared errors, and ask the optimization tool (your choice) to minimize it.
x = patternsearch(@err,x0)


Sign in to comment.

More Answers (2)

Alan Weiss
Alan Weiss on 4 Mar 2013
Edited: Alan Weiss on 4 Mar 2013
If your first run with lsqcurvefit produced a local solution, as it seems, then there is no reason I know that MultiStart would fail every time. I would look into the syntax you used to call MultiStart. This example show how to use lsqcurvefit with MultiStart. In particular, you might need to set bounds for the MultiStart object.
Also, the exit message you quoted (Local minimum possible) means that lsqcurvefit did not obtain a sufficiently low value of its first-order optimality measure, but it might have succeeded in minimizing the model discrepancy anyway. It is not clear from that message whether you need MultiStart, but if you want a global optimum then it is a good idea to use it.
I would avoid using patternsearch or any other solver just yet, lsqcurvefit should provide more accurate answers if you can get it to work with MultiStart.
Alan Weiss
MATLAB mathematical toolbox documentation


Faezeh Manesh
Faezeh Manesh on 30 Apr 2020
Dear Alan,
Actually, I am dealing with the same issue that I can't get desirable results out of my curve-fitting problem. I used lsqcurvefit in the first pplace. Then based on your suggestion, I added MultiStart to my code as well but my result didn't change. I don't know why MATLAB gives the unknown model coefficient the same value as I entered for MATLAB. I bring my code in the following along with the results. I would really appreciate it if you can help me fix this problem and get better results:
clear all;
close all;
T = 294:0.1:361;
for i=1:size(T,1)
f1(i)= 0.2099*exp(-((T(i)-340.2)/2.439).^2);
%Initial value for unknown coefficient
B0 =[-6.1e+04];
%find the local fit using lsqcurvefit
options = optimoptions('lsqcurvefit','FiniteDifferenceType','central','OptimalityTolerance',1e-12,'FunctionTolerance',1e-12, 'MaxFunctionEvaluations',1500);
lb = [-Inf];
ub = [Inf];
[B,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@firstorderDSC1,B0,T,f1,lb,ub,options);
% Set up the problem for MultiStart
problem = createOptimProblem('lsqcurvefit','x0',B0,'objective',@firstorderDSC1,...
ms = MultiStart('PlotFcns',@gsplotbestf);
[xmulti,errormulti] = run(ms,problem,50);
%plot the result
legend('Data','Fitted result')
And here is my @firstorderDSC1 function:
function dndt2 = firstorderDSC1(B, T)
%my differential equation
function denaturate = firstorderDSC(T, n)
a= (exp(183.8)/1.5); %(1/s)
%solving my differential equation
options = odeset('AbsTol', 1e-8,'RelTol',1e-8);%,'OutputFcn',@odeplot);
[temp,num] = ode23s(@firstorderDSC, T, 0, options);
for i=1:size(num,1)
dndt2(i,1) = firstorderDSC(temp(i),num(i));
And here are my results:
Deniz Toprak
Deniz Toprak on 26 Jul 2020
Hi Alan and Shashank,
I have problem with lsqcurvefit function. I couldn't get good results from parameters. I used multistart with lsqcurvefit. The results are worse than before. I mean without multistart results are more approximate to the real parameter. What the problem should be?
How can I get good parameter results ?
Please help me with this?

Sign in to comment.

Alex on 5 Mar 2013
Hi Alan and Shashank,
I solved the problem following the example that Alan posted. Thanks to your comments it is more clear for me not only MultiStart function but also pattersearch.
Many thanks for your help!


Sign in to comment.


Community Treasure Hunt

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

Start Hunting!