# NMinimize doesn't work with Defined function and data set

I have a data set

data={{-35., 0.315382}, {-30., 0.510487}, {-25., 0.808823}, {-20.,
1.25604}, {-15., 1.91404}, {-10., 2.86533}, {-5., 4.21811}, {0.,
6.11213}, {5., 8.7253}, {10., 12.2811}, {15., 17.0568}, {20.,
23.3919}, {25., 31.6982}, {30., 42.4692}, {35., 56.2906}, {40.,
73.8511}, {45., 95.9534}, {50., 123.525}, {55., 157.628}, {60.,
199.474}, {65., 250.427}, {70., 312.022}, {75., 385.967}, {80.,
474.158}, {85., 578.681}, {90., 701.827}, {95., 846.09}, {100.,
1014.18}, {105., 1209.02}, {110., 1433.77}, {115., 1691.8}, {120.,
1986.71}}


and a function

f[t_, a_, b_, c_] := Exp[a + b/(c + t)];


Now I do the NMinimize to find parameters a, b, c by using command:

NMinimize[
Total[((f[data[[All, 1]], a, b, c] - data[[All, 2]])/
data[[All, 2]])^2], {a, b, c}]


The output parameters are wrong. Please let me know what is the problem? Why NMinimize give wrong results.

Thank you

• What is the "right" optimum? Mar 24, 2020 at 12:25

## 2 Answers

Try to scale your data!

dataS = Map[{#[[1]]/Max[data[[All, 1]]],#[[2]]/Max[data[[All, 2]]]} &, data];
f[t_, a_, b_, c_ ] := Exp[a + b/(c + t)];
fit = NonlinearModelFit[dataS, f[t, a, b, c],{a, b, c }, t];
fit["BestFitParameters"]


The fit (scaled coordinates):

Show[{ListPlot[dataS],Plot[{Normal[fit] }, {t, Min[dataS[[All, 1]]],Max[dataS[[All, 1]]]}]}]


• Dear Ulrich Neumann, Thank you for your support. With you rescale strategy, the parameters for data[[16,1]] and above is very good but there is a large relative differences for the first few data. Is there any strategy to balance the relative difference for the beginning and ending data? The starting point for fitting parameters of 12, -4026, and 235.5 maybe possible but I don't know how to make it in NMinimize. Can you have any suggestions
– Anh
Mar 24, 2020 at 14:42
• @Anh Perhaps you can introduce some weights in your NMinimize- approach. Mar 24, 2020 at 15:48
data = {{-35., 0.315382}, {-30., 0.510487}, {-25., 0.808823}, {-20.,
1.25604}, {-15., 1.91404}, {-10., 2.86533}, {-5., 4.21811}, {0.,
6.11213}, {5., 8.7253}, {10., 12.2811}, {15., 17.0568}, {20.,
23.3919}, {25., 31.6982}, {30., 42.4692}, {35., 56.2906}, {40.,
73.8511}, {45., 95.9534}, {50., 123.525}, {55., 157.628}, {60.,
199.474}, {65., 250.427}, {70., 312.022}, {75., 385.967}, {80.,
474.158}, {85., 578.681}, {90., 701.827}, {95., 846.09}, {100.,
1014.18}, {105., 1209.02}, {110., 1433.77}, {115., 1691.8}, {120.,
1986.71}};

{tmin, tmax} = MinMax@data[[All, 1]];

f[t_, a_, b_, c_] := Exp[a + b/(c + t)];


You can provide initial estimates for the model parameters.

fit = NonlinearModelFit[data, f[t, a, b, c],
{{a, 10}, {b, -2000}, {c, 100}}, t];

fit["BestFitParameters"]

(* {a -> 18.6202, b -> -3835.44, c -> 227.859} *)

Show[
ListPlot[data, PlotStyle -> Red],
Plot[fit[t], {t, tmin, tmax}]]


Show[
ListPlot[data, PlotStyle -> Red, ScalingFunctions -> "Log"],
LogPlot[fit[t], {t, tmin, tmax}]]


EDIT: You can get a slightly better fit by first taking the Log of the data

fit2 = NonlinearModelFit[
{#[[1]], Log[#[[2]]]} & /@ data, Log[f[t, a, b, c]],
{{a, 10}, {b, -2000}, {c, 100}}, t];

fit2["BestFitParameters"]

(* {a -> 19.0604, b -> -4098.33, c -> 237.649} *)

Show[
ListPlot[data, PlotStyle -> Red],
Plot[Exp@fit2@t, {t, tmin, tmax}]]


Show[
ListPlot[data, PlotStyle -> Red, ScalingFunctions -> "Log"],
LogPlot[Exp@fit2@t, {t, tmin, tmax}]]


• Thank you for your suggestion. The relative errors between calculated data and original data show the large deviation in the small data range and a very small deviation in higher data range. I prefer to balance the deviation in both small and big data range. That the reason I try with NMinimize or Minimize but doesn't work fine. Do you have any suggestion? Thank you.
– Anh
Mar 24, 2020 at 15:44