4
$\begingroup$

I have a certain unsightly list of point which I want to fit with the following function a^2 + b^2 + 2 abCos[q*d], where a and b are the fit parameters.

When I use NonLinearModelFit to fit my list, it does not work. Most answers on this topic point out the fact that one should give maximum information to NonlinearModelFit, in order for the algorithm to give the right answer. Giving initial "conditions" for the a and b parameters, I could arrive through trial and error at reasonable fits. However, without these initial conditions, the algorithm gives obviously false results (by several orders of magnitude). The problem is that I will probably generate lots of such lists in the futur, and I cannot spend time giving the right initial conditions, (what's more these conditions will most likely change by some order of magnitudes.)

Is there a way to produce good results in a more systematic way ? In particular, can anyone find a good fit for the example below without specifying the initial conditions ?

Thank you very much for your help Regards.

Here is the code :

datax = {{-5.905249348852994`*^6, 
   1.2219735903677265`*^-35}, {-5.846196855364464`*^6, 
   1.2221708107930695`*^-35}, {-5.787144361875934`*^6, 
   1.2227273769309198`*^-35}, {-5.728091868387404`*^6, 
   1.223642754102148`*^-35}, {-5.669039374898874`*^6, 
   1.2249160533016568`*^-35}, {-5.6099868814103445`*^6, 
   1.2265460324538995`*^-35}, {-5.550934387921814`*^6, 
   1.2285310973330108`*^-35}, {-5.491881894433284`*^6, 
   1.2308693032349603`*^-35}, {-5.4328294009447545`*^6, 
   1.233558356823158`*^-35}, {-5.373776907456225`*^6, 
   1.236595618727146`*^-35}, {-5.314724413967694`*^6, 
   1.2399781055473467`*^-35}, {-5.255671920479164`*^6, 
   1.2437024933435424`*^-35}, {-5.196619426990635`*^6, 
   1.2477651206454564`*^-35}, {-5.137566933502105`*^6, 
   1.2521619920013003`*^-35}, {-5.078514440013574`*^6, 
   1.256888782156119`*^-35}, {-5.019461946525045`*^6, 
   1.261940840103639`*^-35}, {-4.960409453036515`*^6, 
   1.267313193765454`*^-35}, {-4.901356959547985`*^6, 
   1.2730005548980968`*^-35}, {-4.842304466059455`*^6, 
   1.2789973242503488`*^-35}, {-4.783251972570925`*^6, 
   1.285297597192664`*^-35}, {-4.724199479082395`*^6, 
   1.2918951694151184`*^-35}, {-4.665146985593865`*^6, 
   1.2987835430493291`*^-35}, {-4.606094492105335`*^6, 
   1.305955933297481`*^-35}, {-4.547041998616805`*^6, 
   1.3134052748241907`*^-35}, {-4.487989505128276`*^6, 
   1.3211242288849756`*^-35}, {-4.428937011639746`*^6, 
   1.3291051906350564`*^-35}, {-4.369884518151215`*^6, 
   1.3373402963452962`*^-35}, {-4.310832024662686`*^6, 
   1.3458214315985846`*^-35}, {-4.251779531174156`*^6, 
   1.354540238829719`*^-35}, {-4.1927270376856257`*^6, 
   1.3634881259980048`*^-35}, {-4.1336745441970956`*^6, 
   1.3726562746403088`*^-35}, {-4.074622050708566`*^6, 
   1.3820356489681034`*^-35}, {-4.015569557220036`*^6, 
   1.3916170045029887`*^-35}, {-3.956517063731506`*^6, 
   1.4013908973426318`*^-35}, {-3.897464570242976`*^6, 
   1.4113476934158427`*^-35}, {-3.838412076754446`*^6, 
   1.4214775780048848`*^-35}, {-3.779359583265916`*^6, 
   1.431770565506127`*^-35}, {-3.7203070897773863`*^6, 
   1.4422165090524098`*^-35}, {-3.661254596288856`*^6, 
   1.4528051107916017`*^-35}, {-3.6022021028003264`*^6, 
   1.463525931930454`*^-35}, {-3.5431496093117963`*^6, 
   1.474368403029108`*^-35}, {-3.4840971158232666`*^6, 
   1.4853218344171682`*^-35}, {-3.4250446223347364`*^6, 
   1.4963754267816589`*^-35}, {-3.3659921288462067`*^6, 
   1.5075182818277147`*^-35}, {-3.3069396353576765`*^6, 
   1.5187394130048713`*^-35}, {-3.247887141869147`*^6, 
   1.5300277562791086`*^-35}, {-3.1888346483806167`*^6, 
   1.5413721812672496`*^-35}, {-3.129782154892087`*^6, 
   1.5527615019755055`*^-35}, {-3.070729661403557`*^6, 
   1.5641844879654804`*^-35}, {-3.011677167915027`*^6, 
   1.575629875433811`*^-35}, {-2.952624674426497`*^6, 
   1.5870863782696638`*^-35}, {-2.8935721809379673`*^6, 
   1.5985426992588261`*^-35}, {-2.834519687449437`*^6, 
   1.6099875411922462`*^-35}, {-2.7754671939609074`*^6, 
   1.6214096180059798`*^-35}, {-2.7164147004723772`*^6, 
   1.632797665960911`*^-35}, {-2.6573622069838475`*^6, 
   1.6441404547043172`*^-35}, {-2.5983097134953174`*^6, 
   1.6554267983980458`*^-35}, {-2.5392572200067877`*^6, 
   1.6666455667298722`*^-35}, {-2.4802047265182575`*^6, 
   1.677785695911094`*^-35}, {-2.421152233029728`*^6, 
   1.6888361995826308`*^-35}, {-2.3620997395411977`*^6, 
   1.6997861796569957`*^-35}, {-2.303047246052668`*^6, 
   1.7106248370911256`*^-35}, {-2.243994752564138`*^6, 
   1.7213414825269563`*^-35}, {-2.184942259075608`*^6, 
   1.73192554682628`*^-35}, {-2.125889765587078`*^6, 
   1.7423665915500432`*^-35}, {-2.0668372720985478`*^6, 
   1.7526543191903989`*^-35}, {-2.007784778610018`*^6, 
   1.762778583402953`*^-35}, {-1.948732285121488`*^6, 
   1.7727293989648223`*^-35}, {-1.8896797916329582`*^6, 
   1.7824969516476264`*^-35}, {-1.830627298144428`*^6, 
   1.7920716079088658`*^-35}, {-1.7715748046558984`*^6, 
   1.80144392441198`*^-35}, {-1.7125223111673682`*^6, 
   1.8106046572849781`*^-35}, {-1.653469817678838`*^6, 
   1.819544771340947`*^-35}, {-1.5944173241903083`*^6, 
   1.8282554489043657`*^-35}, {-1.5353648307017786`*^6, 
   1.83672809853486`*^-35}, {-1.476312337213249`*^6, 
   1.8449543635437838`*^-35}, {-1.4172598437247183`*^6, 
   1.8529261302012246`*^-35}, {-1.3582073502361886`*^6, 
   1.8606355357737587`*^-35}, {-1.299154856747659`*^6, 
   1.8680749762669407`*^-35}, {-1.2401023632591292`*^6, 
   1.875237113891051`*^-35}, {-1.1810498697705986`*^6, 
   1.8821148843802286`*^-35}, {-1.121997376282069`*^6, 
   1.88870150392919`*^-35}, {-1.0629448827935392`*^6, 
   1.8949904758427525`*^-35}, {-1.0038923893050095`*^6, 
   1.9009755970185487`*^-35}, {-944839.8958164789`, 
   1.906650964060857`*^-35}, {-885787.4023279492`, 
   1.9120109790211078`*^-35}, {-826734.9088394195`, 
   1.917050355018528`*^-35}, {-767682.4153508898`, 
   1.9217641214243475`*^-35}, {-708629.9218623592`, 
   1.926147628795655`*^-35}, {-649577.4283738295`, 
   1.9301965533699143`*^-35}, {-590524.9348852998`, 
   1.9339069014332303`*^-35}, {-531472.4413967701`, 
   1.9372750132255289`*^-35}, {-472419.94790823944`, 
   1.9402975665575611`*^-35}, {-413367.45441970974`, 
   1.9429715800760866`*^-35}, {-354314.96093118005`, 
   1.9452944161929393`*^-35}, {-295262.46744265035`, 
   1.9472637837439226`*^-35}, {-236209.97395411972`, 
   1.9488777402165936`*^-35}, {-177157.48046559002`, 
   1.950134693546132`*^-35}, {-118104.98697706033`, 
   1.9510334040620924`*^-35}, {-59052.4934885297`, 
   1.9515729851789904`*^-35}, {0.`, 
   1.9517529047051493`*^-35}, {59052.4934885297`, 
   1.9515729851839438`*^-35}, {118104.9869770594`, 
   1.951033404070347`*^-35}, {177157.48046559002`, 
   1.9501346935675896`*^-35}, {236209.97395411972`, 
   1.9488777401852434`*^-35}, {295262.4674426494`, 
   1.947263783727429`*^-35}, {354314.9609311791`, 
   1.945294416197885`*^-35}, {413367.45441970974`, 
   1.9429715800727913`*^-35}, {472419.94790823944`, 
   1.940297566542744`*^-35}, {531472.4413967691`, 
   1.9372750132452702`*^-35}, {590524.9348852988`, 
   1.933906901471035`*^-35}, {649577.4283738295`, 
   1.9301965533846934`*^-35}, {708629.9218623592`, 
   1.9261476288022163`*^-35}, {767682.4153508889`, 
   1.9217641214292627`*^-35}, {826734.9088394186`, 
   1.9170503550037993`*^-35}, {885787.4023279492`, 
   1.9120109790276454`*^-35}, {944839.8958164789`, 
   1.9066509640722818`*^-35}, {1.0038923893050086`*^6, 
   1.9009755970381042`*^-35}, {1.0629448827935383`*^6, 
   1.894990475863904`*^-35}, {1.121997376282069`*^6, 
   1.8887015039568042`*^-35}, {1.1810498697705986`*^6, 
   1.882114884362392`*^-35}, {1.2401023632591283`*^6, 
   1.875237113887814`*^-35}, {1.299154856747658`*^6, 
   1.8680749762653253`*^-35}, {1.3582073502361886`*^6, 
   1.8606355357624732`*^-35}, {1.4172598437247183`*^6, 
   1.8529261302366205`*^-35}, {1.476312337213248`*^6, 
   1.8449543635052535`*^-35}, {1.5353648307017777`*^6, 
   1.8367280985140357`*^-35}, {1.5944173241903083`*^6, 
   1.8282554488835898`*^-35}, {1.653469817678838`*^6, 
   1.819544771339353`*^-35}, {1.7125223111673677`*^6, 
   1.8106046572977018`*^-35}, {1.7715748046558984`*^6, 
   1.8014439243977026`*^-35}, {1.830627298144428`*^6, 
   1.7920716079579159`*^-35}, {1.8896797916329578`*^6, 
   1.78249695162869`*^-35}, {1.9487322851214875`*^6, 
   1.7727293989663958`*^-35}, {2.007784778610018`*^6, 
   1.7627785834170766`*^-35}, {2.0668372720985478`*^6, 
   1.752654319204482`*^-35}, {2.1258897655870775`*^6, 
   1.7423665915313213`*^-35}, {2.184942259075607`*^6, 
   1.731925546812281`*^-35}, {2.243994752564138`*^6, 
   1.7213414824757824`*^-35}, {2.3030472460526675`*^6, 
   1.7106248371220436`*^-35}, {2.362099739541197`*^6, 
   1.699786179650832`*^-35}, {2.421152233029727`*^6, 
   1.6888361995380865`*^-35}, {2.4802047265182575`*^6, 
   1.677785695918749`*^-35}, {2.5392572200067863`*^6, 
   1.6666455667222428`*^-35}, {2.598309713495318`*^6, 
   1.6554267984254192`*^-35}, {2.6573622069838475`*^6, 
   1.644140454736144`*^-35}, {2.7164147004723772`*^6, 
   1.632797665942787`*^-35}, {2.775467193960907`*^6, 
   1.621409618010495`*^-35}, {2.8345196874494366`*^6, 
   1.609987541165251`*^-35}, {2.8935721809379663`*^6, 
   1.5985426992812418`*^-35}, {2.952624674426496`*^6, 
   1.5870863782681745`*^-35}, {3.0116771679150257`*^6, 
   1.575629875410073`*^-35}, {3.0707296614035573`*^6, 
   1.5641844879595674`*^-35}, {3.129782154892087`*^6, 
   1.5527615019578316`*^-35}, {3.1888346483806167`*^6, 
   1.54137218126138`*^-35}, {3.2478871418691464`*^6, 
   1.5300277562820326`*^-35}, {3.306939635357676`*^6, 
   1.5187394129932183`*^-35}, {3.365992128846206`*^6, 
   1.5075182818552878`*^-35}, {3.4250446223347355`*^6, 
   1.4963754268047924`*^-35}, {3.484097115823265`*^6, 
   1.4853218343984418`*^-35}, {3.5431496093117967`*^6, 
   1.4743684029960993`*^-35}, {3.6022021028003264`*^6, 
   1.4635259319519022`*^-35}, {3.661254596288856`*^6, 
   1.4528051107916017`*^-35}, {3.720307089777386`*^6, 
   1.4422165090453125`*^-35}, {3.7793595832659155`*^6, 
   1.4317705655004695`*^-35}, {3.838412076754445`*^6, 
   1.4214775780316595`*^-35}, {3.897464570242975`*^6, 
   1.4113476934242678`*^-35}, {3.9565170637315065`*^6, 
   1.4013908973482286`*^-35}, {4.015569557220036`*^6, 
   1.391617004494623`*^-35}, {4.074622050708566`*^6, 
   1.3820356489514294`*^-35}, {4.1336745441970956`*^6, 
   1.3726562746541564`*^-35}, {4.1927270376856253`*^6, 
   1.3634881259924845`*^-35}, {4.251779531174155`*^6, 
   1.3545402388751143`*^-35}, {4.310832024662685`*^6, 
   1.3458214315848728`*^-35}, {4.369884518151214`*^6, 
   1.3373402963507636`*^-35}, {4.428937011639746`*^6, 
   1.329105190618705`*^-35}, {4.487989505128276`*^6, 
   1.3211242288863341`*^-35}, {4.547041998616805`*^6, 
   1.313405274807936`*^-35}, {4.606094492105335`*^6, 
   1.3059559332853244`*^-35}, {4.665146985593865`*^6, 
   1.2987835430641462`*^-35}, {4.724199479082394`*^6, 
   1.291895169382876`*^-35}, {4.783251972570924`*^6, 
   1.285297597199364`*^-35}, {4.842304466059454`*^6, 
   1.2789973242770826`*^-35}, {4.901356959547985`*^6, 
   1.2730005549047648`*^-35}, {4.960409453036515`*^6, 
   1.267313193797388`*^-35}, {5.019461946525045`*^6, 
   1.261940840114261`*^-35}, {5.078514440013574`*^6, 
   1.2568887822011727`*^-35}, {5.137566933502104`*^6, 
   1.252161991993365`*^-35}, {5.196619426990634`*^6, 
   1.2477651206454564`*^-35}, {5.2556719204791635`*^6, 
   1.243702493348815`*^-35}, {5.314724413967693`*^6, 
   1.2399781055473467`*^-35}, {5.373776907456225`*^6, 
   1.2365956187034875`*^-35}, {5.4328294009447545`*^6, 
   1.2335583568284092`*^-35}, {5.491881894433284`*^6, 
   1.2308693032061116`*^-35}, {5.550934387921814`*^6, 
   1.2285310973054993`*^-35}, {5.609986881410344`*^6, 
   1.2265460324748438`*^-35}, {5.669039374898873`*^6, 
   1.2249160533186625`*^-35}, {5.728091868387403`*^6, 
   1.2236427540916884`*^-35}, {5.7871443618759345`*^6, 
   1.2227273769531382`*^-35}, {5.846196855364464`*^6, 
   1.2221708108218161`*^-35}, {5.905249348852994`*^6, 
   1.2219735904003909`*^-35}};

fit = NonlinearModelFit[
  datax, {a^2 + b^2 + 2 a*b*Cos[q*d]}, { {a, 3.96*10^-18}, {b, 
    0.45*10^-18}}, q];

fit["BestFitParameters"];

Show[ListPlot[data], Plot[fit[q], {q, -\[Pi]/d, \[Pi]/d}], 
 Frame -> True];
$\endgroup$
  • 2
    $\begingroup$ It is a good idea to scale your formulas so that the best fit parameters would be on the order of magnitude of 1, and also all parameters would have similar orders of magnitudes. Do the same with the function and variable values as well. If this is physics/science, then simply choose reasonable units: don't measure the size of bacteria in kilometres. $\endgroup$ – Szabolcs Apr 14 at 11:29
  • $\begingroup$ Is d one of your fitting parameters? $\endgroup$ – mmeent Apr 14 at 23:51
6
$\begingroup$

I do not think that it is a reasonable request to do the fit without initial parameter values when you are working with such extreme numbers. The optimization algorithms will typically need to choose an initial value anyway, and with non-linear models, there will usually be multiple local minima.

The solution is to use reasonable units. You have numbers comparable to $10^{-35}$ for the function value and $10^6$ for the variable. Choose reasonable units for expressing these values! Ideally, your values should have the order of magnitude of 1.

The same comment applies to parameter values. Include constants so that the best fit values would have the order of magnitude of 1. In other words, use reasonable units.

data = {10^-6 #1, 10^35 #2} & @@@ datax;

fit = NonlinearModelFit[data, {a^2 + b^2 + 2 a*b*Cos[q d]}, {a, b, d}, q,
  Method -> NMinimize]

Here I used Method -> NMinimize as it will not get stuck in local minima.

Show[
 ListPlot[data, PlotStyle -> Black],
 Plot[fit[q], {q, -6, 6}, 
  PlotStyle -> Directive[AbsoluteThickness[1], Pink]]
 ]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ thank you for your answer $\endgroup$ – yfs Apr 14 at 12:03
4
$\begingroup$

You need to rescale your data as Scabolcs suggests. I tried without rescaling and the fit did not work. So try

data1 = datax /. {x_, y_} -> {x 10^-6, y 10^35};

Show[ListPlot[data1],
 Plot[Evaluate[
   a^2 + b^2 + 2 a*b*Cos[q*d] /. {a -> 1/Sqrt[2], b -> 1/Sqrt[2], 
     d -> π/12}], {q, -6, 6}]

Mathematica graphics ]

I have made guesses at the fit that seem about right. Do they work? Here goes:

fit = NonlinearModelFit[
   data1, {a^2 + b^2 + 2 a*b*Cos[q*d]}, {{a, 1/Sqrt[2]}, {b, 
     1/Sqrt[2]}, {d, \[Pi]/12}}, q];
Show[ListPlot[data1, PlotStyle -> Green], 
 Plot[fit[q], {q, -6 , 6}, PlotStyle -> Red], Frame -> True]

Mathematica graphics

The fit is perfect so I guess this is perfect data.

In order to progress we need to know something about how far different to you example the data will be. I suggest you try a few data sets with the initial guess I made and then see how that works.

| improve this answer | |
$\endgroup$
  • $\begingroup$ thank you for your answer $\endgroup$ – yfs Apr 14 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.