# Help with NonlinearModelFit Error

I would like to ask if anybody can help me to find a way around solving this problem in Mathematica. I've tried solutions I found here but they don't seem to work. I need all the help I can get. Thanks in advance!

This is my dataset:

data={{-0.5,0.657639},{-0.498043,0.667265},{-0.496086,0.676743},{-0.494129,0.686072},{-0.492172,0.695252},{-0.490215,0.704282},{-0.488258,0.713165},{-0.486301,0.721898},{-0.484344,0.730482},{-0.482387,0.738918},{-0.480431,0.747206},{-0.478474,0.755347},{-0.476517,0.76334},{-0.47456,0.771187},{-0.472603,0.778888},{-0.470646,0.786442},{-0.468689,0.793852},{-0.466732,0.801117},{-0.464775,0.808238},{-0.462818,0.815216},{-0.460861,0.822051},{-0.458904,0.828745},{-0.456947,0.835296},{-0.45499,0.841708},{-0.453033,0.84798},{-0.451076,0.854113},{-0.449119,0.860109},{-0.447162,0.865966},{-0.445205,0.871687},{-0.443249,0.877273},{-0.441292,0.882723},{-0.439335,0.888039},{-0.437378,0.893221},{-0.435421,0.898271},{-0.433464,0.90319},{-0.431507,0.907978},{-0.42955,0.912635},{-0.427593,0.917162},{-0.425636,0.921562},{-0.423679,0.925833},{-0.421722,0.929977},{-0.419765,0.933996},{-0.417808,0.937889},{-0.415851,0.941656},{-0.413894,0.9453},{-0.411937,0.94882},{-0.40998,0.952219},{-0.408023,0.955495},{-0.406067,0.958649},{-0.40411,0.961683},{-0.402153,0.964597},{-0.400196,0.967391},{-0.398239,0.970067},{-0.396282,0.972625},{-0.394325,0.975065},{-0.392368,0.977387},{-0.390411,0.979594},{-0.388454,0.981685},{-0.386497,0.983659},{-0.38454,0.98552},{-0.382583,0.987265},{-0.380626,0.988897},{-0.378669,0.990415},{-0.376712,0.99182},{-0.374755,0.993113},{-0.372798,0.994293},{-0.370841,0.995361},{-0.368885,0.996319},{-0.366928,0.997164},{-0.364971,0.9979},{-0.363014,0.998525},{-0.361057,0.999039},{-0.3591,0.999443},{-0.357143,0.999738},{-0.355186,0.999924},{-0.353229,1.},{-0.351272,0.999967},{-0.349315,0.999825},{-0.347358,0.999575},{-0.345401,0.999216},{-0.343444,0.998748},{-0.341487,0.998172},{-0.33953,0.997488},{-0.337573,0.996695},{-0.335616,0.995794},{-0.333659,0.994785},{-0.331703,0.993666},{-0.329746,0.992439},{-0.327789,0.991105},{-0.325832,0.989661},{-0.323875,0.988109},{-0.321918,0.986447},{-0.319961,0.984677},{-0.318004,0.982797},{-0.316047,0.980808},{-0.31409,0.978709},{-0.312133,0.9765},{-0.310176,0.974181},{-0.308219,0.971751},{-0.306262,0.969211},{-0.304305,0.96656},{-0.302348,0.963797},{-0.300391,0.960921},{-0.298434,0.957934},{-0.296477,0.954834},{-0.294521,0.951621},{-0.292564,0.948294},{-0.290607,0.944853},{-0.28865,0.941297},{-0.286693,0.937626},{-0.284736,0.93384},{-0.282779,0.929938},{-0.280822,0.925918},{-0.278865,0.921781},{-0.276908,0.917527},{-0.274951,0.913154},{-0.272994,0.908662},{-0.271037,0.904049},{-0.26908,0.899317},{-0.267123,0.894463},{-0.265166,0.889488},{-0.263209,0.88439},{-0.261252,0.879168},{-0.259295,0.873823},{-0.257339,0.868353},{-0.255382,0.862759},{-0.253425,0.857037},{-0.251468,0.851189},{-0.249511,0.845214},{-0.247554,0.83911},{-0.245597,0.832876},{-0.24364,0.826513},{-0.241683,0.82002},{-0.239726,0.813395},{-0.237769,0.806638},{-0.235812,0.799747},{-0.233855,0.792724},{-0.231898,0.785566},{-0.229941,0.778274},{-0.227984,0.770845},{-0.226027,0.76328},{-0.22407,0.755578},{-0.222114,0.747738},{-0.220157,0.73976},{-0.2182,0.731642},{-0.216243,0.723385},{-0.214286,0.714988},{-0.212329,0.70645},{-0.210372,0.69777},{-0.208415,0.688949},{-0.206458,0.679985},{-0.204501,0.67088},{-0.202544,0.66163},{-0.200587,0.652238},{-0.19863,0.642701},{-0.196673,0.633021},{-0.194716,0.623197},{-0.192759,0.613229},{-0.190802,0.603116},{-0.188845,0.59286},{-0.186888,0.58246},{-0.184932,0.571916},{-0.182975,0.561228},{-0.181018,0.550398},{-0.179061,0.539425},{-0.177104,0.528311},{-0.175147,0.517055},{-0.17319,0.505659},{-0.171233,0.494124},{-0.169276,0.482451},{-0.167319,0.470641},{-0.165362,0.458695},{-0.163405,0.446615},{-0.161448,0.434403},{-0.159491,0.42206},{-0.157534,0.409588},{-0.155577,0.39699},{-0.15362,0.384268},{-0.151663,0.371423},{-0.149706,0.358459},{-0.14775,0.345379},{-0.145793,0.332185},{-0.143836,0.31888},{-0.141879,0.305468},{-0.139922,0.291951},{-0.137965,0.278334},{-0.136008,0.26462},{-0.134051,0.250813},{-0.132094,0.236917},{-0.130137,0.222935},{-0.12818,0.208872},{-0.126223,0.194733},{-0.124266,0.180522},{-0.122309,0.166244},{-0.120352,0.151903},{-0.118395,0.137504},{-0.116438,0.123052},{-0.114481,0.108552},{-0.112524,0.0940087},{-0.110568,0.0794281},{-0.108611,0.064815},{-0.106654,0.0501747},{-0.104697,0.0355125},{-0.10274,0.0208338},{-0.100783,0.0061438},{-0.0988258,-0.0085519},{-0.0968689,-0.023248},{-0.0949119,-0.0379392},{-0.092955,-0.0526201},{-0.090998,-0.0672853},{-0.0890411,-0.0819295},{-0.0870841,-0.0965475},{-0.0851272,-0.111134},{-0.0831703,-0.125684},{-0.0812133,-0.140193},{-0.0792564,-0.154654},{-0.0772994,-0.169065},{-0.0753425,-0.183419},{-0.0733855,-0.197712},{-0.0714286,-0.211939},{-0.0694716,-0.226096},{-0.0675147,-0.240179},{-0.0655577,-0.254183},{-0.0636008,-0.268104},{-0.0616438,-0.281938},{-0.0596869,-0.295681},{-0.0577299,-0.30933},{-0.055773,-0.32288},{-0.053816,-0.336329},{-0.0518591,-0.349672},{-0.0499022,-0.362907},{-0.0479452,-0.376031},{-0.0459883,-0.38904},{-0.0440313,-0.401932},{-0.0420744,-0.414704},{-0.0401174,-0.427354},{-0.0381605,-0.439879},{-0.0362035,-0.452276},{-0.0342466,-0.464545},{-0.0322896,-0.476682},{-0.0303327,-0.488685},{-0.0283757,-0.500554},{-0.0264188,-0.512286},{-0.0244618,-0.523879},{-0.0225049,-0.535333},{-0.0205479,-0.546646},{-0.018591,-0.557816},{-0.0166341,-0.568843},{-0.0146771,-0.579725},{-0.0127202,-0.590462},{-0.0107632,-0.601053},{-0.00880626,-0.611498},{-0.00684932,-0.621795},{-0.00489237,-0.631943},{-0.00293542,-0.641944},{-0.000978474,-0.651796},{0.000978474,-0.661499},{0.00293542,-0.671053},{0.00489237,-0.680457},{0.00684932,-0.689712},{0.00880626,-0.698818},{0.0107632,-0.707775},{0.0127202,-0.716583},{0.0146771,-0.725242},{0.0166341,-0.733752},{0.018591,-0.742114},{0.0205479,-0.750329},{0.0225049,-0.758396},{0.0244618,-0.766316},{0.0264188,-0.774089},{0.0283757,-0.781717},{0.0303327,-0.789199},{0.0322896,-0.796536},{0.0342466,-0.80373},{0.0362035,-0.810778},{0.0381605,-0.817685},{0.0401174,-0.82445},{0.0420744,-0.831073},{0.0440313,-0.837556},{0.0459883,-0.843898},{0.0479452,-0.850102},{0.0499022,-0.856166},{0.0518591,-0.862094},{0.053816,-0.867885},{0.055773,-0.87354},{0.0577299,-0.87906},{0.0596869,-0.884445},{0.0616438,-0.889697},{0.0636008,-0.894816},{0.0655577,-0.899804},{0.0675147,-0.904661},{0.0694716,-0.909387},{0.0714286,-0.913985},{0.0733855,-0.918453},{0.0753425,-0.922794},{0.0772994,-0.927008},{0.0792564,-0.931097},{0.0812133,-0.93506},{0.0831703,-0.938899},{0.0851272,-0.942614},{0.0870841,-0.946206},{0.0890411,-0.949676},{0.090998,-0.953024},{0.092955,-0.956251},{0.0949119,-0.959359},{0.0968689,-0.962347},{0.0988258,-0.965216},{0.100783,-0.967968},{0.10274,-0.970602},{0.104697,-0.97312},{0.106654,-0.975521},{0.108611,-0.977807},{0.110568,-0.979977},{0.112524,-0.982034},{0.114481,-0.983977},{0.116438,-0.985806},{0.118395,-0.987522},{0.120352,-0.989126},{0.122309,-0.990618},{0.124266,-0.991999},{0.126223,-0.993268},{0.12818,-0.994427},{0.130137,-0.995476},{0.132094,-0.996414},{0.134051,-0.997243},{0.136008,-0.997963},{0.137965,-0.998574},{0.139922,-0.999076},{0.141879,-0.99947},{0.143836,-0.999754},{0.145793,-0.999931},{0.14775,-1.},{0.149706,-0.999962},{0.151663,-0.999815},{0.15362,-0.999561},{0.155577,-0.9992},{0.157534,-0.998731},{0.159491,-0.998155},{0.161448,-0.997472},{0.163405,-0.996682},{0.165362,-0.995784},{0.167319,-0.994779},{0.169276,-0.993666},{0.171233,-0.992447},{0.17319,-0.991119},{0.175147,-0.989684},{0.177104,-0.988141},{0.179061,-0.986491},{0.181018,-0.984733},{0.182975,-0.982865},{0.184932,-0.98089},{0.186888,-0.978805},{0.188845,-0.976612},{0.190802,-0.974309},{0.192759,-0.971896},{0.194716,-0.969374},{0.196673,-0.966741},{0.19863,-0.963998},{0.200587,-0.961144},{0.202544,-0.958178},{0.204501,-0.9551},{0.206458,-0.95191},{0.208415,-0.948607},{0.210372,-0.945191},{0.212329,-0.94166},{0.214286,-0.938016},{0.216243,-0.934257},{0.2182,-0.930382},{0.220157,-0.926391},{0.222114,-0.922284},{0.22407,-0.91806},{0.226027,-0.913718},{0.227984,-0.909258},{0.229941,-0.904678},{0.231898,-0.899978},{0.233855,-0.895159},{0.235812,-0.890218},{0.237769,-0.885156},{0.239726,-0.879971},{0.241683,-0.874662},{0.24364,-0.869229},{0.245597,-0.863673},{0.247554,-0.85799},{0.249511,-0.852182},{0.251468,-0.846246},{0.253425,-0.840184},{0.255382,-0.833992},{0.257339,-0.827671},{0.259295,-0.82122},{0.261252,-0.81464},{0.263209,-0.807927},{0.265166,-0.801082},{0.267123,-0.794104},{0.26908,-0.786993},{0.271037,-0.779747},{0.272994,-0.772366},{0.274951,-0.764849},{0.276908,-0.757196},{0.278865,-0.749406},{0.280822,-0.741478},{0.282779,-0.733412},{0.284736,-0.725207},{0.286693,-0.716863},{0.28865,-0.708378},{0.290607,-0.699753},{0.292564,-0.690987},{0.294521,-0.682079},{0.296477,-0.673029},{0.298434,-0.663838},{0.300391,-0.654503},{0.302348,-0.645026},{0.304305,-0.635407},{0.306262,-0.625644},{0.308219,-0.615738},{0.310176,-0.605688},{0.312133,-0.595496},{0.31409,-0.585161},{0.316047,-0.574684},{0.318004,-0.564063},{0.319961,-0.553301},{0.321918,-0.542398},{0.323875,-0.531354},{0.325832,-0.52017},{0.327789,-0.508846},{0.329746,-0.497385},{0.331703,-0.485786},{0.333659,-0.474051},{0.335616,-0.462181},{0.337573,-0.450178},{0.33953,-0.438044},{0.341487,-0.425779},{0.343444,-0.413386},{0.345401,-0.400867},{0.347358,-0.388224},{0.349315,-0.37546},{0.351272,-0.362576},{0.353229,-0.349575},{0.355186,-0.33646},{0.357143,-0.323234},{0.3591,-0.3099},{0.361057,-0.296461},{0.363014,-0.282921},{0.364971,-0.269282},{0.366928,-0.255548},{0.368885,-0.241724},{0.370841,-0.227813},{0.372798,-0.213819},{0.374755,-0.199746},{0.376712,-0.185598},{0.378669,-0.17138},{0.380626,-0.157097},{0.382583,-0.142753},{0.38454,-0.128353},{0.386497,-0.113901},{0.388454,-0.0994029},{0.390411,-0.0848633},{0.392368,-0.0702873},{0.394325,-0.05568},{0.396282,-0.0410468},{0.398239,-0.0263928},{0.400196,-0.0117233},{0.402153,0.00295631},{0.40411,0.0176408},{0.406067,0.0323247},{0.408023,0.0470028},{0.40998,0.0616696},{0.411937,0.07632},{0.413894,0.0909485},{0.415851,0.10555},{0.417808,0.120119},{0.419765,0.134651},{0.421722,0.149141},{0.423679,0.163583},{0.425636,0.177973},{0.427593,0.192305},{0.42955,0.206576},{0.431507,0.220781},{0.433464,0.234914},{0.435421,0.248972},{0.437378,0.262951},{0.439335,0.276846},{0.441292,0.290653},{0.443249,0.304368},{0.445205,0.317988},{0.447162,0.331509},{0.449119,0.344928},{0.451076,0.358241},{0.453033,0.371444},{0.45499,0.384536},{0.456947,0.397512},{0.458904,0.41037},{0.460861,0.423108},{0.462818,0.435723},{0.464775,0.448212},{0.466732,0.460573},{0.468689,0.472805},{0.470646,0.484904},{0.472603,0.496869},{0.47456,0.508699},{0.476517,0.520392},{0.478474,0.531945},{0.480431,0.543358},{0.482387,0.55463},{0.484344,0.565759},{0.486301,0.576743},{0.488258,0.587583},{0.490215,0.598277},{0.492172,0.608825},{0.494129,0.619225},{0.496086,0.629477},{0.498043,0.639581},{0.5,0.649537}}


I'm trying to find a way to fit these to the data:

{σ0, ω} = {99.7525, 2 π};
sol=ParametricNDSolveValue[{D[( 3^(1/3) g^(2/3)-2^(1/3) (-9 Sqrt[cg] σs[t]+Sqrt[12 g^2+81 cg σs[t]^2])^(2/3))/(6^(2/3) Sqrt[cg] (g (-9 Sqrt[cg] σs[t]+Sqrt[12 g^2+81 cg σs[t]^2]))^(1/3)),t]==(2 3^(1/3) η^(2/3)-2^(1/3) (Sqrt[12 η^2+81 cη (99.7525 Sin[2 π t]-σs[t])^2]-9 Sqrt[cη] (99.7525 Sin[2 π t]-σs[t]))^(2/3))/(6^(2/3) Sqrt[cη] (η (Sqrt[12 η^2+81 cη (99.7525 Sin[2 π t]-σs[t])^2]-9 Sqrt[cη] (99.7525 Sin[2 π t]-σs[t])))^(1/3)),σs[-π/ω]==σs[π/ω]},σs,{t,-π/ω,π/ω},{g,η,cg,cη}];

tmodel= g (sol + cg (sol)^3)[g, η, cg, cη][t]


and then using NonlinearModelFit

fit = NonlinearModelFit[data,tmodel,{g,η,cg,cη},t,Method->"NMinimize"]


But I always get the error

NonlinearModelFit::nrnum: The function value is not a real number at {}={}.


I hope somebody can help me with this. Thank you very much!

• Not a solution but a start: try tmodel[g_, η_, cg_, cη_, t_] = g (sol[g, η, cg, cη][t] + cg (sol[g, η, cg, cη][t])^3) and then fit = NonlinearModelFit[data, tmodel[g, η, cg, cη, t], {g, η, cg, cη}, t, Method -> "NMinimize"]. May 10, 2019 at 8:39

To make this work correctly, you need to make sure that tmodel evaluates in the correct order. First the numerical values of the parameters g, η, cg, cη, t should be substituted to evaluate the ParametricFunction to a InterpolatingFunction. Then numerical values for t should be substituted. This can be achieved as follows:

Clear[tmodel]
tmodel[{g_, \[Eta]_, cg_, c\[Eta]_}?(VectorQ[#, NumericQ] &)] :=
tmodel[{g, \[Eta], cg, c\[Eta]}] =
With[{
intFun = sol[g, \[Eta], cg, c\[Eta]]
},
Function[t, g (intFun[t] + cg (intFun[t])^3)]
];


By memoizing the function tmodel, we can prevent unnecessary re-evaluations of the InterpolatingFunction. Note also that tmodel returns a Function of t.

Test the function:

tmodel[{1, 1, 1, 1}][{0, 0.5}]


{-29.9616, 40.365}

The fit is now called as follows:

fit = NonlinearModelFit[data,  tmodel[{g, \[Eta], cg, c\[Eta]}][t],
{g, \[Eta], cg, c\[Eta]}, t, Method -> "NMinimize"]


Unfortunately, this seems to produce a lot of errors because NonlinearModelFit tries parameter values that produce imaginary function values. The next step would be to define reasonable constraints on {g, η, cg, cη} to prevent NonlinearModelFit from venturing into the wrong regions of parameter space.