1
$\begingroup$

If I have the following data:

Tgexpandlit={{57.8505, 1.36339}, {58.1254, 1.0671}, {58.7645, 0.646116}, {59.221, 
  0.349873}, {60.6788, -0.273668}, {62.7712, -0.803456}, {64.3186, \
-1.28659}, {64.3186, -1.28659}, {66.5927, -1.83193}, {68.7745, \
-2.20572}, {71.0471, -2.57948}, {72.7748, -2.92218}, {74.3203, \
-3.20255}, {76.138, -3.45164}, {78.5006, -3.74739}, {80.2275, \
-3.99651}, {81.4994, -4.12096}, {82.6805, -4.24544}, {83.4074, \
-4.32324}, {84.1346, -4.44783}, {85.1342, -4.57236}, {86.3147, \
-4.61885}, {87.1328, -4.75901}, {88.2229, -4.85232}, {88.859, \
-4.93014}, {90.04, -5.03902}, {91.4019, -5.06986}, {92.2198, \
-5.17884}, {93.4007, -5.28772}, {93.3998, -5.17853}, {94.4905, \
-5.34983}, {96.4889, -5.50529}, {97.3069, -5.62986}, {99.1234, \
-5.75417}, {99.2131, -5.62936}, {99.8498, -5.76958}, {101.848, \
-5.87825}, {101.847, -5.83145}, {102.575, -5.95605}, {103.936, \
-5.98689}, {105.389, -6.0489}, {53.05, 1.02729}, {54.3782, 
  0.553702}, {54.2158, 
  0.0303926}, {56.9221, -0.439578}, {59.0303, -0.956926}, {62.3764, \
-1.42534}, {66.8545, -1.9367}, {69.3497, -2.40747}, {70.7512, \
-2.70496}, {71.6037, -2.92466}, {51.2734, 1.02255}, {52.1209, 
  0.547695}, {52.3608, 
  0.0254565}, {55.4547, -0.443448}, {56.8651, -0.962607}, {59.8218, \
-1.43198}, {65.7229, -1.9396}, {69.0066, -2.40835}, {70.6466, \
-2.70522}, {71.5932, -2.92468}, {54.5032, 1.03115}, {54.9453, 
  0.555205}, {56.6674, 
  0.0368729}, {57.758, -0.437381}, {60.2665, -0.9537}, {62.7824, \
-1.42429}, {66.8842, -1.93663}, {69.7442, -2.40648}, {71.3554, \
-2.70343}, {72.963, -2.92124}, {53.05, 1.02729}, {54.4775, 
  0.553965}, {54.7589, 
  0.0318322}, {58.2357, -0.436128}, {60.1001, -0.954134}, {63.2519, \
-1.42308}, {66.8886, -1.93662}, {69.3662, -2.40743}, {70.8179, \
-2.70479}, {71.7396, -2.92431}, {51.2734, 1.02255}, {52.2206, 
  0.547961}, {52.8968, 
  0.0268856}, {56.701, -0.44016}, {57.9147, -0.959849}, {60.6854, \
-1.42973}, {65.7529, -1.93952}, {69.0215, -2.40831}, {70.7068, \
-2.70507}, {71.7159, -2.92437}, {54.5032, 1.03115}, {55.0455, 
  0.55547}, {57.1738, 
  0.0382057}, {59.0075, -0.434107}, {61.2621, -0.95111}, {63.6165, \
-1.42214}, {66.9159, -1.93655}, {69.7599, -2.40644}, {71.4196, \
-2.70327}, {73.0906, -2.92092}, {51.139, 2.32322}, {52.2163, 
  1.80322}, {53.1749, 1.32866}, {53.6977, 1.02902}, {54.1641, 
  0.808406}, {54.7846, 0.554779}, {55.0134, 0.333536}, {50.6163, 
  2.32182}, {52.0102, 1.80267}, {52.6447, 1.32724}, {53.6972, 
  1.02902}, {54.1492, 0.808367}, {54.1878, 0.553197}, {54.8392, 
  0.333075}, {52.6762, 2.32733}, {53.7473, 1.8073}, {54.0918, 
  1.33109}, {55.3849, 1.03349}, {55.5279, 0.812018}, {55.6434, 
  0.557051}, {55.9373, 0.335978}}

and I am fitting it to a function such as:

   Eappitz = 99.925; q0 = 10/60; Tg0itz = 54.26 + 273.15; Aitz = 
 Log10[(Tg0itz^2)/(q0*Eappitz*1000)];

(nlm = NonlinearModelFit[
    Tgexpandlit, {((-C11 (T + 273.15 - Tg0itz))/(C22 + (T + 273.15 - 
            Tg0itz))) + Aitz}, {C11, C22}, T]) // Normal

nlm["BestFitParameters"]

C1itz = C11 /. nlm["BestFitParameters"](*7.76*);
C2itz = C22 /. nlm["BestFitParameters"](*19.22*);


fitz[T_] := ((-C1itz (T + 273.15 - Tg0itz))/(C2itz + (T + 273.15 - 
        Tg0itz))) + Aitz

I get a very bad fit such as the following:

Show[Plot[fitz[T], {T, -5, 108}, Frame -> True, FrameStyle -> 16, 
  Axes -> False, GridLines -> Automatic, 
  GridLinesStyle -> Lighter[Gray, .8], 
  FrameTicks -> {Automatic, Automatic}, ImageSize -> Large, 
  LabelStyle -> {Black, Bold, 14}, PlotStyle -> Blue, 
  PlotRange -> {All, {-6.4, 6}}]
 
 , ListPlot[Tgexpandlit]]

Which gives:

enter image description here

However, If I only use the first portion of the same data such as:

Tgexpandlit={{57.8505, 1.36339}, {58.1254, 1.0671}, {58.7645, 0.646116}, {59.221, 
  0.349873}, {60.6788, -0.273668}, {62.7712, -0.803456}, {64.3186, \
-1.28659}, {64.3186, -1.28659}, {66.5927, -1.83193}, {68.7745, \
-2.20572}, {71.0471, -2.57948}, {72.7748, -2.92218}, {74.3203, \
-3.20255}, {76.138, -3.45164}, {78.5006, -3.74739}, {80.2275, \
-3.99651}, {81.4994, -4.12096}, {82.6805, -4.24544}, {83.4074, \
-4.32324}, {84.1346, -4.44783}, {85.1342, -4.57236}, {86.3147, \
-4.61885}, {87.1328, -4.75901}, {88.2229, -4.85232}, {88.859, \
-4.93014}, {90.04, -5.03902}, {91.4019, -5.06986}, {92.2198, \
-5.17884}, {93.4007, -5.28772}, {93.3998, -5.17853}, {94.4905, \
-5.34983}, {96.4889, -5.50529}, {97.3069, -5.62986}, {99.1234, \
-5.75417}, {99.2131, -5.62936}, {99.8498, -5.76958}, {101.848, \
-5.87825}, {101.847, -5.83145}, {102.575, -5.95605}, {103.936, \
-5.98689}, {105.389, -6.0489}}

I now get a great fit like:

enter image description here

Question:

  1. Why cannot use all the data or how can I arrange the data so that all the data can be used to get the fit? (as opposed to only part of the same data)
$\endgroup$
3
  • 2
    $\begingroup$ You need better starting guesses for your fit, which you can get from your partial fit. Try {{C11, 17}, {C22, 70}} as the parameter specification in your NonlinearModelFit. The importance of providing good starting values for parameters in fitting cannot be overstated. $\endgroup$
    – MarcoB
    Commented Jan 4, 2021 at 3:52
  • 2
    $\begingroup$ I think you also have other data/model issues. It appears that you've concatenated 9 different datasets with each of those maybe following the same basic model but with different parameters. If so, please describe how the data is generated and why there are 9 different datasets. Each dataset likely requires some common and some different parameters. $\endgroup$
    – JimB
    Commented Jan 4, 2021 at 4:24
  • $\begingroup$ @MarcoB thank you! You are right, I used the starting values and it worked!. I was manipulating the data in all sort of ways but it was the starting values for the fitting all along!. $\endgroup$
    – John
    Commented Jan 4, 2021 at 4:48

2 Answers 2

8
$\begingroup$

It appears that you have 9 different sets of data probably all under slightly different conditions. You really need to account for that. If you explained that here, you might get an appropriate solution. Otherwise, you might try CrossValidated and ask about nonlinear mixed models (along with how the data was collected).

In the meantime I'll just expand on why @MarcoB 's advice is essential.

With not-so-good starting values the iterative algorithm won't converge or you'll land on a local minimum rather than the global minimum (assuming there is a global minimum) of the total sum of squares.

The default starting value is 1 if you don't specify a starting value. Here are the results for using @MarcoB 's starting values and what happens when the default starting values are used:

nlm = NonlinearModelFit[Tgexpandlit, 
  {((-C11 (T + 273.15 - Tg0itz))/(C22 + (T + 273.15 - Tg0itz))) + Aitz},
  {{C11, 17}, {C22, 70}}, T];
nlm["BestFitParameters"]
(* {C11 -> 13.5547, C22 -> 48.2289} *) 

nlm1 = NonlinearModelFit[Tgexpandlit, 
  {((-C11 (T + 273.15 - Tg0itz))/(C22 + (T + 273.15 - Tg0itz))) + Aitz},
  {{C11, 1}, {C22, 1}}, T];
nlm1["BestFitParameters"]
(* {C11 -> 1.81295, C22 -> 0.754181} *)

Because there are only 2 parameters to estimate we can construct a contour plot of the sum of squares for values of C11, and C22 showing the starting and ending values:

ss = Total[(Tgexpandlit[[All, 2]] - (((-C11 (Tgexpandlit[[All, 1]] + 273.15 - 
  Tg0itz))/(C22 + (Tgexpandlit[[All, 1]] + 273.15 - Tg0itz))) + Aitz))^2];

Show[ContourPlot[ss, {C11, 0.5, 30}, {C22, 0.1, 100}, 
  ImageSize -> Large,
  Contours -> {25, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 
    500, 600, 700, 800, 900, 1000, 2000},
  ScalingFunctions -> {"Log", "Log"}, ContourShading -> None, 
  ContourStyle -> LightGray,
  FrameLabel -> (Style[#, 18, Bold] &) /@ {"C11", "C22"}, 
  PlotPoints -> 100],
 ListLogLogPlot[{
   {{17, 70}},
   {{C11, C22} /. nlm["BestFitParameters"]},
   {{C11, C22} /. nlm["BestFitParameters"], {17, 70}},
   {{1, 1}},
   {{C11, C22} /. nlm1["BestFitParameters"]},
   {{C11, C22} /. nlm1["BestFitParameters"], {1, 1}}
   }, PlotStyle -> {Green, Red, Black, Green, Red, Black},
  Joined -> {False, False, True, False, False, True},
  PlotLegends -> {"Starting value", "Ending value"}]]

Contour plot of sum of squares

One can see that there are several local minima that one will land on depending on the starting values.

$\endgroup$
2
  • $\begingroup$ JimB this is Fantastic and thank you for the explanation !!! $\endgroup$
    – John
    Commented Jan 4, 2021 at 21:12
  • $\begingroup$ You're welcome. But there is "something" about your data that I'm curious about. Not only does there appear to be 9 separate data sets but there are lots of groups of "almost ties" among the responses: ListPlot[Sort[Tgexpandlit]]. I ask because to get a proper fit requires knowledge about how the data was generated. $\endgroup$
    – JimB
    Commented Jan 5, 2021 at 5:16
3
$\begingroup$

With a simple rational model together with Method -> "NMinimize" (no need for starting values!) the approximation is quite well!

Try

nlm = NonlinearModelFit[Tgexpandlit, {a + b/(1 + c T)}, {a, b, c}, T,Method -> "NMinimize"]  

Show[{ListPlot[Tgexpandlit], Plot[nlm[T], {T, 50, 110}]},PlotRange -> All]

enter image description here

$\endgroup$
1
  • $\begingroup$ Ulrich thank you very much! $\endgroup$
    – John
    Commented Jan 4, 2021 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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