I want to understand the rule under which the function Nonlinearfit
stops to give the final result. I know that if the number of iterations exceeds MaxIterations
it stops, but otherwise it should give the best fit of the data for the given model.
In my experience instead it stops sometimes far before finding the best fit. One proof is that increasing the number of fitting parameters the quality of the fit sometimes decreases (often dramatically).
Consider for example the data in http://pastebin.com/raw.php?i=KqRHKE0pqþ and the program
minN = 1; maxN = 8;
g[x_,xo_,σ_,a_]:=a Exp[-((x-xo)^2/(2 σ^2))]
kvar[k_Integer]:=ToExpression@Map[StringJoin[#,ToString[k]]&,{"x","σ","a"}]
gmodel[n_Integer] := const + Sum[g[x, Sequence @@ kvar[i]], {i, 1, n}]
gpars[n_Integer]:=Flatten@{const,Array[kvar,n]}
myFittedModelsU1={};
Do[{
Print[kT];
tmpM=NonlinearModelFit[myData,gmodel[kT],gpars[kT],x,Method->{NMinimize,Method->{"DifferentialEvolution","ScalingFactor"->0.9,"CrossProbability"->0.7,"PostProcess"->{FindMinimum,Method->"QuasiNewton"}}} ];
AppendTo[myFittedModelsU1,tmpM];
},{kT,minN,maxN,1}]
The program tries to fit the data with model given by a mixture of Gaussian whose number changes for minN
to maxN
.
Increasing the number of parameters should increase the agreement between the model and the data (that does not mean the quality of the fit, of course), but for example in this case the fit is better for N=7 gaussians than for 10. Why this happens and how could I avoid this effect and obtain the best fit for my model?
NonlinearModelFit
. Together with them you give initial conditions. It is right in the doc and easy to see. I suggest that you have a look at the whole doc on fitting... Seema you didn't do that $\endgroup$WorkingPrecision
. Example for kT=3 $\endgroup$