# NonlinearModelFit a custom function - getting wrong fit parameters and huge std

Im trying to Fit some measured Data, but the fitted parameters which the FindFit[] Ffunction is returning are completly off.

I have the following Data depending on the Temperature

Data = {0.625571, 0.680365, 0.744292, 0.799087, 0.863014, 0.899543, 0.945205, 0.972603, 1., 0.995434, 0.977169, 0.972603, 0.926941,0.894977, 0.817352, 0.767123, 0.707763, 0.621005, 0.59589, 0.484018,0.408676, 0.340183, 0.278539, 0.221461, 0.180822, 0.138813, 0.10137, 0.0794521, 0.0593607, 0.0474886, 0.0383562}
Temperatur := {30., 35., 40., 45., 50., 55., 60., 65., 70., 75., 80., 85., 90., 95., 100., 105., 110., 115., 120., 125., 130., 135., 140., 145., 150., 155., 160., 165., 170., 175., 180.}


And I need to Fit the following Model to the Data:

\[Eta]fit[T_, \[Lambda]fit_, \[CapitalLambda]_, lfit_, a_] = a*(Sinc[(dkQ[T, \[Lambda]fit, \[CapitalLambda]]*L[T, lfit])/2])^2


where as dkQ[...] and L[...] are equal to

dkQ[T_, \[Lambda]_, \[CapitalLambda]_] := (4*Pi)*(ne[T, \[Lambda]/2] - ne[T, \[Lambda]])/\[Lambda] - (2*Pi)/(\[CapitalLambda]*Exp[\[Alpha]*(T - T0)])
L[T_, L0_] := L0*(1 + \[Alpha]*(T - T0))
ne[T_, \[Lambda]_] := Sqrt[(5.356 +4.63*10^(-7)*(f[T]) + ((0.1005 +  3.86*10^(-8)*(f[T]))/(\[Lambda]^2 - (0.21 -  0.89*10^(-8)*(f[T]))^2)) + ((100 + 2.66*10^(-5)*(f[T]))/(\[Lambda]^2 - 11.35^2) - 1.53*10^(-2)*\[Lambda]^2))];
f[T_] := (T - T0)*(T + 570.82)


I also have the following constants / variables:

\[CapitalLambda]c1 := {18.4, 18.6, 18.8, 19.0, 19.2}
\[Lambda]0 := 1.56
L0 := 1000
T0 := 22
\[Alpha] := 1.54*10^-5


Next I used the follwoing to create the Fit

dL := 500
d\[Lambda] := 0.0119
TestFit = FindFit[Data, {\[Eta]fit[T, \[Lambda]fit, \[CapitalLambda]c1[[5]], lfit, a], (\[Lambda]0 - d\[Lambda]) < \[Lambda]fit < (\[Lambda]0 + d\[Lambda]), (L0 - dL) < lfit < (L0 + dL) }, {{a, 1}, {\[Lambda]fit, \[Lambda]0}, {lfit, L0}}, T]


The result I got was:

{a -> 1., \[Lambda]fit -> 1.56945, lfit -> 500.}


But when I plot the model with the fitted parameters I got this plot, but as you can see the fitted line does not match the data at all

But when I choose the Parameters below by hand, the model fits way better, but nowhere near as perfect as it needs to be, so doing all the work by hand would take ages ^^

{a -> 1., \[Lambda]fit -> 1.566783, lfit -> 850.}


I also tried the NonlinearModelFit[...] functions but with that, I get the same Values and huge standarddeviations for lfit.

I dont really know what I could improve, the fit function already has the right borders for the parameters and so... maybe Im missing something important

Hopefully you can help me out and thanks already :) If there is any Information missing, I would be happy to add them of course ;)

• Crossposted here. Apr 7, 2021 at 15:50

There are some inconsistencies between the code you show (which doesn't run) and the results you obtained.

First, your model function in FindFit is currently missing the parameter a. Second, your data should include the temperature values, i.e. it should be built up as a list of pairs. Finally, as a rule of thumb use := (SetDelayed) to define functions, and = (Set) for constants such as Data, Temperatur, L0, T0... Also avoid variable names starting with an uppercase letter; these may conflict with built-ins.

Here is my approach:

xyData = Transpose[{Temperatur, Data}];

fit = NonlinearModelFit[
xyData,
{
ηfit[T, λfit, Λc1[[5]], lfit, a],
(λ0 - dλ) < λfit < (λ0 + dλ),
(L0 - dL) < lfit < (L0 + dL)
},
{{a, 1}, {λfit, λ0}, {lfit, 850}}, T
];

fit["BestFitParameters"]

(* Out: {a -> 0.976434, λfit -> 1.56681, lfit -> 761.764} *)

Show[
ListPlot[xyData],
Plot[fit[T], {T, 30, 180}],
PlotRange -> All
]


• Thank you very much! It´s my first time using Mathematica so thats kinda the excuse for the awful syntax ^^ The only mistake was actually the missing Transpose of the Data. How could I miss that... Apr 7, 2021 at 15:11