# Fitting does not work

I have a set of points, which is basically one minus a Lorentz curve. And the NonlinearModelFit

 nlm = NonlinearModelFit[
S11, ((f - A)^2 + (C - B)^2/4)/((f - A)^2 + (C + B)^2/4), {A, B, C}, f]


Here is the desired fraction of sample data:

    {{-1., 0.898594}, {-0.985454, 0.895599}, {-0.970909,
0.891564}, {-0.956363, 0.890405}, {-0.941818, 0.886959}, {-0.927272,
0.884573}, {-0.912727, 0.881205}, {-0.898181,
0.881556}, {-0.883636, 0.872478}, {-0.86909, 0.876878}, {-0.854545,
0.872316}, {-0.839999, 0.870754}, {-0.825454, 0.86436}, {-0.810909,
0.865351}, {-0.796363, 0.848462}, {-0.781818, 0.857101}, {-0.767272,
0.851719}, {-0.752727, 0.848654}, {-0.738181,
0.841293}, {-0.723636, 0.839157}, {-0.70909, 0.821046}, {-0.694545,
0.829559}, {-0.68, 0.824545}, {-0.665454, 0.817557}, {-0.650909,
0.812038}, {-0.636363, 0.807831}, {-0.621818, 0.778687}, {-0.607272,
0.79112}, {-0.592727, 0.786824}, {-0.578181, 0.77672}, {-0.563636,
0.771804}, {-0.54909, 0.761295}, {-0.534545, 0.75221}, {-0.519999,
0.741318}, {-0.505454, 0.735782}, {-0.490909, 0.722225}, {-0.476363,
0.715568}, {-0.461818, 0.697832}, {-0.447272,
0.689986}, {-0.432727, 0.672385}, {-0.418181, 0.663297}, {-0.403636,
0.643117}, {-0.38909, 0.634264}, {-0.374545, 0.610815}, {-0.359999,
0.598726}, {-0.345454, 0.573733}, {-0.330909,
0.562218}, {-0.316363, 0.53201}, {-0.301818, 0.517783}, {-0.287272,
0.462347}, {-0.272727, 0.465476}, {-0.258181, 0.43969}, {-0.243636,
0.414656}, {-0.22909, 0.384142}, {-0.214545, 0.342314}, {-0.2,
0.322577}, {-0.185454, 0.297302}, {-0.170909, 0.260666}, {-0.156363,
0.233593}, {-0.141818, 0.196899}, {-0.127272, 0.16879}, {-0.112727,
0.135725}, {-0.098181, 0.113781}, {-0.083636,
0.0819164}, {-0.06909, 0.0645791}, {-0.054545,
0.0383557}, {-0.039999, 0.0260247}, {-0.025454,
0.0104437}, {-0.010909, 0.0048168}, {0.003637,
0.000813105}, {0.018182, 0.000228645}, {0.032728,
0.000223204}, {0.047273, 0.00113846}, {0.061819,
0.00523423}, {0.076364, 0.00926002}, {0.09091,
0.0201436}, {0.105455, 0.0285999}, {0.120001, 0.049829}, {0.134546,
0.0621246}, {0.149091, 0.0878749}, {0.163637, 0.10922}, {0.178182,
0.138383}, {0.192728, 0.164309}, {0.207273, 0.197518}, {0.221819,
0.227506}, {0.236364, 0.258341}, {0.25091, 0.290622}, {0.265455,
0.320149}, {0.28, 0.353528}, {0.294546, 0.38427}, {0.309091,
0.415339}, {0.323637, 0.441874}, {0.338182, 0.471903}, {0.352728,
0.494983}, {0.367273, 0.524311}, {0.381819, 0.544424}, {0.396364,
0.570566}, {0.41091, 0.587294}, {0.425455, 0.613763}, {0.440001,
0.62776}, {0.454546, 0.6504}, {0.469091, 0.664721}, {0.483637,
0.683155}, {0.498182, 0.695034}, {0.512728, 0.713271}, {0.527273,
0.722182}, {0.541819, 0.739027}, {0.556364, 0.746874}, {0.57091,
0.759936}, {0.585455, 0.768587}, {0.600001, 0.77884}, {0.614546,
0.787491}, {0.629091, 0.798301}, {0.643637, 0.805192}, {0.658182,
0.812616}, {0.672728, 0.81989}, {0.687273, 0.82687}, {0.701819,
0.830344}, {0.716364, 0.83873}, {0.73091, 0.84281}, {0.745455,
0.850219}, {0.76, 0.853569}, {0.774546, 0.857741}, {0.789091,
0.862166}, {0.803637, 0.866199}, {0.818182, 0.869372}, {0.832728,
0.874231}, {0.847273, 0.876242}, {0.861819, 0.87831}, {0.876364,
0.881898}, {0.89091, 0.8845}, {0.905455, 0.888046}, {0.92,
0.890699}, {0.934546, 0.894051}, {0.949091, 0.896532}, {0.963637,
0.89834}, {0.978182, 0.902531}, {0.992728, 0.902106}}


Here is the result so far:

If you look at the b and c estimation, they are exactly identical, and identically junk. What might have caused this?

• Welcome to Mathematica.SE! I formatted your code for you (actually so did someone else but I barged in without seeing the suggested edit :-/) Have you tried changing your parameter names to lowercase letters? Single uppercase letters can clash with things. Dec 6, 2015 at 21:37
• Hello. I just tried that, but it did not help. Same FittedModel. I also tried a different Method, which was in this case NMinimize. By the way: How did you format the code that way? Dec 6, 2015 at 21:43
• Probably normalizing the x and y values making them run in the 0 - 1 range will help Dec 6, 2015 at 22:05
• Indent lines with 4 spaces to create a code block. Or use the {} button in the editor. Or select the code and hit ctrl-k Dec 6, 2015 at 22:11
• I suspect that it might be a problem with numerical precision, considering the y-range of your data. Please post sample values for S11 so we can try to figure it out, otherwise we are just guessing. Dec 6, 2015 at 22:22

Update The numerical precision can be increased to show a slight difference in the estimates of B and C:

nlm = NonlinearModelFit[
Rationalize[S11, 0], ((f - A)^2 + (C - B)^2/4)/((f - A)^2 + (C + B)^2/4),
{A, B, C}, f, WorkingPrecision -> 30];

(* Parameter Estimates *)
N[nlm["BestFitParameters"], 30]
(* {A -> 0.0289023002665553437482132571671775096752704349239253238439,
B -> 0.3245570808578016666078310334682285501227010637490946667114,
C -> 0.3245570808578017485113197281528207381066076977062742841326} *)

(* Correlation between estimators of B and C *)
nlm["CorrelationMatrix"][[2, 3]]
(* -0.9999999999999999999999999999998629805404272935773837940987 *)

(* Standard errors of the estimates of B and C *)
nlm["CovarianceMatrix"][[2, 2]]^(1/2)
(* 5.0240325807363789533461791516557051236610970785949 *)
nlm["CovarianceMatrix"][[3, 3]]^(1/2)
(* 5.02403258073638160831475622192042881084177250385839 *)


We see that the correlation coefficient for the estimates of B and C is nearly -1.0 suggesting a numerical issue when B is close to C. (The covariance matrix is nearly singular.)

Such numerical issues occur when B is close to C. Below is a Manipulate that generates a new random sample whenever A, B, or C is changed followed by an estimation of the underlying curve. We see that the correlation goes to nearly -1 and the standard error estimates get very large when B is close to C.

Manipulate[
(* Generate a new set of random errors whenever any parameter is changed *)
error = RandomVariate[NormalDistribution[0, 2/100], 2 n + 1];
y = eq[x, a, b, c];
data = Rationalize[Transpose[{x, y + error}], 0];
nlm = NonlinearModelFit[data, eq[z, aa, bb, cc], {aa, bb, cc}, z,
WorkingPrecision -> 30];

Show[Plot[eq[w, aa, bb, cc] /. nlm["BestFitParameters"], {w, -2, 2},
Frame -> True, PlotRange -> {{-2, 2}, {0, 1}},
FrameLabel -> {{"y", ""}, {"x",
"Correlation of b and c =" <>
ToString[nlm["CorrelationMatrix"][[2, 3]]] <> "\n" <>
"Std.Err[\!$$\*OverscriptBox[\(b$$, $$^$$]\)]=" <>
ToString[FortranForm[nlm["CovarianceMatrix"][[2, 2]]^0.5]] <>
"\n" <>
"St.Err[\!$$\*OverscriptBox[\(c$$, $$^$$]\)]=" <>
ToString[FortranForm[nlm["CovarianceMatrix"][[3, 3]]^0.5]]}}],
ListPlot[Transpose[{x, y + error}]]],
{{a, 0}, -4, 4, Appearance -> "Labeled"},
{{b, 0.3}, 0, 3, Appearance -> "Labeled"},
{{c, 0.3}, 0, 3, Appearance -> "Labeled"},
TrackedSymbols :> {a, b, c},
Initialization :> (n = 40;
x = Table[2 i/n, {i, -n, n}];
eq[x_, a_, b_, c_] := ((x - a)^2 + (b - c)^2/4)/((x - a)^2 + (b + c)^2/4))]


End of Update

This is just an extended comment based on what @george2079 recommended. (In essence I'm just spelling out @george2079's hint.)

If you expand the numerator and denominator of your equation using

Expand[((f - A)^2 + (C-B)^2/4)]/Expand[((f - A)^2 + (C + B)^2/4)]


you'll get

From that you can see that the model is "oddly" parameterized in that the numerator and denominator are quadratic equations with intercepts constructed with more complexity than needed. An equivalent model that results in separate estimates for the 3 parameters in can be written as

eq = (d - 2 a f + f^2)/(d + e - 2 a f + f^2)
nlm = NonlinearModelFit[data, eq, {a, d, e}, f]
nlm["BestFitParameters"]
(* {a -> 0.029016, d -> -0.00177491, e -> 0.102504} *)
Show[ListPlot[data], Plot[nlm[x], {x, -1, 1}], Frame -> True]


• Just a note to say that when this issue happens in this case because the data supports B being close to C and the estimators of B and C are highly positively correlated. This can result either odd looking estimates for B and C and/or their associated standard errors. But the predictions will be just fine.
– JimB
Dec 7, 2015 at 21:58
• It's not my day today to accurate. The estimators of B and C are highly negatively correlated when B and C are nearly equal (rather than positive as I say above).
– JimB
Dec 8, 2015 at 0:40

Without your actual data it's hard to be sure, but rescaling and shifting the data points may help.

With this fake data:

S11 = Block[{A = 2995, B = 0.3, C = 0.3},
Table[{f,
RandomReal[2*^10] +
10^12 ((f - A)^2 + (C - B)^2/4)/((f - A)^2 + (C + B)^2/4)},
{f, 2994, 2996, 0.02}]];


we get the same problem:

nlm = NonlinearModelFit[S11, ((f - A)^2 + (C - B)^2/4)/((f - A)^2 + (C + B)^2/4),
{A, B, C}, f]


data = S11 /. {f_, val_} :> {f - 2995, val/10^12};


and assisting Mathematica with a starting estimate for A

nlm = NonlinearModelFit[data, ((f - A)^2 + (C - B)^2/4)/((f - A)^2 + (C + B)^2/4),
{{A, 0}, B, C}, f];


we get a decent fit:

Plot[10^12 nlm[f - 2995], {f, 2994, 2996}, PlotRange -> All, Epilog -> Point@S11]


• Hello. Thank you so far for your advices. You enabled me to fit at least something, that looks like the original. Now the problem is the parameter estimation. I get the following result for the parameters: picload.org/image/ppcgaca/neuebitmap.png Dec 7, 2015 at 13:42