# NonlinearModelFit for nonlinear functions

I am trying to find a nonlinear model from the data.

My code is below:

data = {{0.0, 0.0}, {0.05, 0.87}, {0.1, 0.99}, {0.15, 0.98}, {0.2,
0.91}, {0.25, 0.81}, {0.3, 0.71}, {0.35, 0.62}, {0.4, 0.51}, {0.45,
0.31}, {0.5, 0.31}, {0.55, 0.23}, {0.6, 0.18}, {0.65, 0.14}, {0.7,
0.08}, {0.75, 0.05}, {0.8, 0.03}, {0.85, 0.02}, {0.9,
0.01}, {0.95, 0.002}, {1, 0}};

model=((1 - x)/(1 - a))^((0.5 (1 - a))/
a) (x/a)^0.5;

(* fit model*)

NonlinearModelFit[data, model, a, x]


NonlinearModelFit doesn't work for this model, i.e.

Are there any other ways to solve this problem?

Update:

If I try:

NonlinearModelFit[data, {model, {a > 0.000001}}, a, x]


Errors:

• What about constrains for a - is it $0<a<1$? Commented Jun 19, 2022 at 16:12
• @AlexTrounev i try NonlinearModelFit[data, {model, {a > 0.000001}}, a, x] but it does not work... Commented Jun 19, 2022 at 16:26
• Just curious: Are the response variables relative frequencies and are the first and last responses (both 0) from observations? Also should the 4th observation be {0.15, 0.98} rather than {0.2, 0.98}?
– JimB
Commented Jun 19, 2022 at 18:59

We can use NMimimize to solve this problem as follows

data = {{0.0, 0.0}, {0.05, 0.87}, {0.1, 0.99}, {0.2,
0.98}, {0.2, 0.91}, {0.25, 0.81}, {0.3, 0.71}, {0.35, 0.62}, {0.4,
0.51}, {0.45, 0.31}, {0.5, 0.31}, {0.55, 0.23}, {0.6,
0.18}, {0.65, 0.14}, {0.7, 0.08}, {0.75, 0.05}, {0.8,
0.03}, {0.85, 0.02}, {0.9, 0.01}, {0.95, 0.002}, {1, 0}};

f[a_, x_] := ((1 - x)/(1 - a))^((0.5 (1 - a))/a) (x/a)^0.5;

vec[a_] = Table[data[[i, 2]] - f[a, data[[i, 1]]], {i, Length[data]}];
sol = NMinimize[{vec[a] . vec[a], 0 < a < 1}, {a}]

(*Out[]= {0.0152585, {a -> 0.127671}}*)


Visualization

Show[Plot[f[a, x] /. sol[[2]], {x, 0, 1}],
ListPlot[data, PlotStyle -> Red]]


• Nice answer! can we still use fit function for this task? Commented Jun 19, 2022 at 16:27
• We can use NonlinearModelFit with option Method -> NMinimize as it shown by xzczd. Commented Jun 19, 2022 at 16:54
nlm = NonlinearModelFit[data, {model, 0 < a < 1}, a, x, Method -> NMinimize]
Plot[nlm[x], {x, 0, 1}]~Show~ListPlot@data


• Very nice answer(+1). How you know about option Method -> NMinimize? :) Commented Jun 19, 2022 at 16:47
• @alex This is mentioned in Details and Options section of NonlinearModelFit: "Possible settings for Method include "ConjugateGradient", "Gradient", "LevenbergMarquardt", "Newton", "NMinimize", and "QuasiNewton", with the default being Automatic. " Also, an example setting Method->NMinimize can be found in Options section of document of FindFit (Yeah, no example in document of NonlinearModelFit, at least for now). But honestly speaking, I learned this when reading certain post in this site, IIRC. :) Commented Jun 19, 2022 at 16:51
• Thank you very much. Commented Jun 19, 2022 at 17:03
• I think it's worth mentioning that NMinimize also takes options. For example Method->{NMinimize, Method->”RandomSearch”} Commented Jun 19, 2022 at 23:57
• @DavidKeith thanks for your suggestions! Commented Jun 20, 2022 at 10:55

The problem seems to stem from the first and last data points, with $$x = 0$$ and $$x = 1$$. My guess is that it has to do with $$\partial f/\partial x$$ being singular at these points. In addition, $$\partial f/\partial a$$ is singular when $$a = 0$$ or $$a = 1$$.

If you remove the offending data points, and give Mathematica an initial guess for $$a$$ that is away from the trouble spots, NonlinearModelFit runs without complaints & yields parameter a -> 0.127671.

newdata = Most[Rest[data]]
fit = NonlinearModelFit[newdata, model, {{a, 0.5}}, x]
Show[ListPlot[newdata, PlotStyle -> Orange], Plot[fit[x], {x, 0, 1}]]


Note that since the model automatically goes through the omitted data points for $$0 < a < 1$$, omitting them shouldn't affect the quality of the fit.

• For whatever it's worth, that's why I suspected that first and last points aren't really data but "theoretical anchors" (for lack of a better term). Also, the curve "shape" is that of a beta distribution and the x values are equally spaced which suggests some sort of binning of the observations (i.e., maybe fitting a probability distribution rather than a regression).
– JimB
Commented Jun 20, 2022 at 15:22