# How to fit an implicit function to data points with errors

**URL:** <https://root-forum.cern.ch/t/how-to-fit-an-implicit-function-to-data-points-with-errors/33708>\
**Category:** ROOT\
**Created:** [April 22, 2019, 9:37am UTC](https://root-forum.cern.ch/t/how-to-fit-an-implicit-function-to-data-points-with-errors/33708 "2019-04-22T09:37:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 22, 2019, 9:37am UTC](https://root-forum.cern.ch/t/how-to-fit-an-implicit-function-to-data-points-with-errors/33708/1 "2019-04-22T09:37:29Z")

</div>

Is there any way in ROOT to fit an “[implicit function](https://en.wikipedia.org/wiki/Implicit_function)” to data points with errors?

This question is related to [a case](https://root-forum.cern.ch/t/fit-does-not-include-all-values/33472/2) in which, I guess, one would need to fit a function in form:

```auto
I - (V - I * Rs) / Rp = Is * (std::exp((V - I * Rs) / (n * Vth)) - 1.)

```

The above function is a non-ideal I-V characteristic of a p-n junction diodes, where “Is” (a scale current), “Rs” (a parasitic series resistance), “Rp” (a parasitic parallel resistance) and “n” (an ideality factor) are the parameters of the function which need to be found by the fit and “Vth” (a thermal voltage) is just a constant (around 25.852 mV @ 300 K). One can choose `x = V` and `y = I` (that’s how these characteristics are usually drawn in the literature) or `x = I` and `y = V` (that’s what is used in [the original thread](https://root-forum.cern.ch/t/fit-does-not-include-all-values/33472/2)). Note that the “experimental data” can have errors in both variables (i.e. “V” and “I”).

See:

> [@Fit does not include all values](https://root-forum.cern.ch/t/fit-does-not-include-all-values/33472/2):
>
> It seems to me that your function is not able to describe your data (in the full range) well. Try this: { // Id-Vd characteristic of a LED // https://root-forum.cern.ch/t/fit-does-not-include-all-values/33472 // experimental data in four columns: Id[A] eId[A] Vd[V] eVd[V] Double\_t IV[][4] = { {6e-08, 5.03e-08, 0.0609, 0.0005609}, {7e-08, 5.035e-08, 0.1718, 0.0006718}, {9e-08, 5.045e-08, 0.2805, 0.0007805}, {1e-07, 5.05e-08, 0.3791, 0.0008791}, {1.1e-07, 5.055e-08, 0.…

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [April 23, 2019, 7:26am UTC](https://root-forum.cern.ch/t/how-to-fit-an-implicit-function-to-data-points-with-errors/33708/2 "2019-04-23T07:26:21Z")

</div>

Hi,

Using a TGraphErrors with both errors on x and y should work in this case.  
Otherwise one can try to fit the data points using a defined implicit function, and computing the 2d distance between points and error and weight that distance with the projected errors.  
For this case one needs to write the least square function directly.  
A similar (but simpler) example is provided in [https://root.cern.ch/doc/master/line3Dfit\_8C.html](https://root.cern.ch/doc/master/line3Dfit_8C.html)  
In this case one fits a 3D object with a 2d function, but the concept is similar, although one would need to consider the point errors

Lorenzo

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 23, 2019, 11:51am UTC](https://root-forum.cern.ch/t/how-to-fit-an-implicit-function-to-data-points-with-errors/33708/3 "2019-04-23T11:51:26Z")

</div>

The problem with an [implicit function](https://en.wikipedia.org/wiki/Implicit_function) is that it is given by an equation `F(x,y)=0` (let’s restrict to a two-dimensional case) and, in general, it cannot be rewritten into a simple explicit form `y=f(x)` nor `x=f(y)` so, no [TF1](https://root.cern.ch/doc/master/classTF1.html) exists, which one could use to fit a [TGraphErrors](https://root.cern.ch/doc/master/classTGraphErrors.html).

If one wants to use some standard minimizer, it is not really obvious how the “chi^2” should be defined. Well, one could simply sum up terms `std:abs(F(x,y))` or `std::pow(F(x,y), 2.)` (for all “experimental data points”) but it is not obvious how to properly take the errors in “x” and “y” into account.

BTW. It seems, for example, that the “[Origin](https://www.originlab.com/)” knows “[non-linear implicit curve fitting](https://www.originlab.com/doc/Origin-Help/Fitting-Implicit)” and it even has a built-in [ModDiode](https://www.originlab.com/doc/Origin-Help/ModDiode-FitFunc) function. The used algorithm is called the “[ODRPACK95: A Weighted OrthogonalDistance Regression Code withBound Constraints](https://www.originlab.com/doc/Origin-Help/NLFit-Theory#Implicit_Functions)” (and it can be used for both implicit and explicit functions). @Axel Maybe it’s worth adding to ROOT. @couet And one would also need to implement the drawing of two-dimensional [implicit functions](https://en.wikipedia.org/wiki/Implicit_function) in ROOT.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 24, 2019, 9:28am UTC](https://root-forum.cern.ch/t/how-to-fit-an-implicit-function-to-data-points-with-errors/33708/4 "2019-04-24T09:28:56Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> @couet And one would also need to implement the drawing of two-dimensional [implicit functions](https://en.wikipedia.org/wiki/Implicit_function) in ROOT.

You mean TF2 ? drawing exists already for TF2

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 8, 2019, 9:34am UTC](https://root-forum.cern.ch/t/how-to-fit-an-implicit-function-to-data-points-with-errors/33708/5 "2019-05-08T09:34:16Z")

</div>

Closed on May 8, 2019, 9:34am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
