# \[?\] Understanding fitting with NonlinearModelFit

**URL:** <https://community.wolfram.com/t/understanding-fitting-with-nonlinearmodelfit/15376>\
**Category:** Questions\
**Tags:** graphics-and-visualization, modeling\
**Created:** [February 13, 2020, 4:31pm UTC](https://community.wolfram.com/t/understanding-fitting-with-nonlinearmodelfit/15376 "2020-02-13T16:31:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![winstondc](https://avatars.discourse-cdn.com/v4/letter/w/b38774/32.png) [@winstondc](https://community.wolfram.com/u/winstondc)\
**Post date:** [February 13, 2020, 4:31pm UTC](https://community.wolfram.com/t/understanding-fitting-with-nonlinearmodelfit/15376/1 "2020-02-13T16:31:59Z")

</div>

I’m trying to get a better handle on fitting (using NonlinearModelFit and Fit), and I can’t understand why my test problem doesn’t work. I’m generating a set of points on a half circle, with some added noise:

```
randomNums = Table[{x, Sqrt[5^2 - x^2] + Random[]}, {x, -5, 5, 0.5}]

```

Next I’m simply trying to fit a nlm to this set of data:

```
nlm = NonlinearModelFit[randomNums, a Sqrt[b r^2 - c x^2] + d, {a, b, c, d, r}, x ]

```

However, this just throws back an error that I don’t understand:

```
NonlinearModelFit::nrlnum: The function value {0.653833 +4.89898 I,-2.09763+4.38748 I,-2.43186+3.87298 I,-2.89856+3.3541 I,-3.65104+2.82843 I,-3.59796+2.29129 I,-3.77146+1.73205 I,-4.11906+1.11803 I,-4.72929+0. I,-3.72242+0. I,-3.8093+0. I,-4.07116+0. I,-4.22766+0. I,-4.74941+1.11803 I,-3.93771+1.73205 I,-4.14349+2.29129 I,-3.93865+2.82843 I,-3.55063+3.3541 I,-2.80604+3.87298 I,-1.3348+4.38748 I,0.0544825 +4.89898 I} is not a list of real numbers with dimensions {21} at {a,b,c,d,r} = {1.,1.,1.,1.,1.}.

```

---

<div class="post-metadata">

**Author:** ![mariuszi28cg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mariuszi28cg/32/308_2.png) [@mariuszi28cg](https://community.wolfram.com/u/mariuszi28cg)\
**Post date:** [February 13, 2020, 6:18pm UTC](https://community.wolfram.com/t/understanding-fitting-with-nonlinearmodelfit/15376/2 "2020-02-13T18:18:08Z")

</div>

Workaround:

```
 ClearAll["`*"]
 randomNums = Table[{x, Sqrt[5^2 - x^2] + Random[]}, {x, -5, 5, 0.5}];
 nlm = NonlinearModelFit[randomNums, a Re[Sqrt[b r^2 - c x^2]] + d, {a, b, c, d, r}, x] // Quiet
 Show[{ListPlot[randomNums, PlotStyle -> Black], Plot[(nlm // Normal), {x, -5, 5}, PlotStyle -> Red]}]

```

 ![2566Bez tytułu.png](https://global.discourse-cdn.com/wolfram/original/3X/6/2/62486247e431ec8090dabf286a292bb78c2025b7.png)

For more information see: [here](https://mathematica.stackexchange.com/questions/31070/problem-with-nonlinearmodelfit-the-function-value-is-not-a-list-of-real-num), [here](https://mathematica.stackexchange.com/questions/38191/how-to-solve-this-findfitnrlnum) and [here](https://mathematica.stackexchange.com/questions/65670/fitting-data-to-a-circle-with-findfit).

---

<div class="post-metadata">

**Author:** ![jbaldwin](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jbaldwin](https://community.wolfram.com/u/jbaldwin)\
**Post date:** [February 13, 2020, 8:00pm UTC](https://community.wolfram.com/t/understanding-fitting-with-nonlinearmodelfit/15376/3 "2020-02-13T20:00:22Z")

</div>

The main issue is that the model is overparameterized as there are really only 3 rather than 5 parameters that can be fit. For example, b and r are always together in the single term b r^2. Maybe one could estimate b r^2 but not b and r separately.

To make the model match the form as to how the data was generated one can rewrite the model as

\left(a \sqrt{c}\right) \sqrt{\frac{b r^2}{c}-x^2}+d

So a\sqrt{c}, \frac{b r^2}{c}, and d can be estimated. To rewrite the model so that `NonlinearModelFit` can work might be

```
SeedRandom[12345]
randomNums = Table[{x, Sqrt[5^2 - x^2] + Random[]}, {x, -5, 5, 0.5}];
nlm = NonlinearModelFit[randomNums, {a Re[Sqrt[b - x^2]] + d, b >= 25},
   {a, {b, 30}, d}, x, MaxIterations -> 5000];
(* {a -> 1.05357, b -> 25.0679, d -> 0.240849} *)

```

I don’t know why you chose a uniform distribution from 0 to 1 as the error function. Usually one assumes that the error distribution has a mean (or median) of zero. (And `Random` has been replaced with `RandomReal` since version 6.)

---

<div class="post-metadata">

**Author:** ![winstondc](https://avatars.discourse-cdn.com/v4/letter/w/b38774/32.png) [@winstondc](https://community.wolfram.com/u/winstondc)\
**Post date:** [February 14, 2020, 12:42pm UTC](https://community.wolfram.com/t/understanding-fitting-with-nonlinearmodelfit/15376/4 "2020-02-14T12:42:21Z")

</div>

Thanks Jim, I realize it was over parameterized, but I was just playing with the fitting functions for practice before working on a real fitting problem.

I don’t understand you comment about the error function, are you referring to my “test data” I made up? Again, I was just trying to understand the syntax and workings of the function, the actual data and fitting here was just a quick example choice.

---

<div class="post-metadata">

**Author:** ![jbaldwin](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jbaldwin](https://community.wolfram.com/u/jbaldwin)\
**Post date:** [February 14, 2020, 8:56pm UTC](https://community.wolfram.com/t/understanding-fitting-with-nonlinearmodelfit/15376/5 "2020-02-14T20:56:10Z")

</div>

I understand that this was test data. I just wanted to point out that typically one chooses an error with a mean of zero. Using `RandomReal[{-0.5,0.5}]` would accomplish this. (Otherwise, the random component also includes a fixed component that probably is best incorporated in the fixed part of the model. Not required but I think easier to interpret.

Overparameterized models do have problems with convergence (even if the predictions are just fine) so examples without overparameterized models don’t have that issue confounding the other issues with model fit.
