# Nonlinear fitting

From Ref. 1,

The equations of state depend nonlinearly of a collection of parameters, $E_0$, $V_0$, $B_0$, $B_0'$, ..., that represent physical properties of the solid at equilibrium and can, in principle, be obtained expermentally by independent methods. The use of a given analytical EOS may have significant influence on the results obtained, particularly because the parameters are far from being independent. The number of parameters has to be considered in comparing the goodness of fit of functional forms with different analytical flexibility. The possibility of using too many parameters, beyond what is physically justified by the information contained in the experimental data, is a serious aspect that deserves consideration.

In `EquationsOfState`

, the nonlinear fitting is currently implemented by `LsqFit`

, a small library that provides basic least-squares fitting in pure Julia. It only utilizes the *Levenberg-Marquardt algorithm* for non-linear fitting. See its documentation for more information.

## Usage

We provide API `lsqfit`

currently.

```
using EquationsOfState.Collections
using EquationsOfState.NonlinearFitting
volumes = [
25.987454833,
26.9045702104,
27.8430241908,
28.8029649591,
29.7848370694,
30.7887887064,
31.814968055,
32.8638196693,
33.9353435494,
35.0299842495,
36.1477417695,
37.2892088485,
38.4543854865,
39.6437162376,
40.857201102,
42.095136449,
43.3579668329,
44.6456922537,
45.9587572656,
47.2973100535,
48.6614988019,
50.0517680652,
51.4682660281,
52.9112890601,
54.3808371612,
55.8775030703,
57.4014349722,
58.9526328669,
];
energies = [
-7.63622156576,
-8.16831294894,
-8.63871612686,
-9.05181213218,
-9.41170988374,
-9.72238224345,
-9.98744832526,
-10.210309552,
-10.3943401353,
-10.5427238068,
-10.6584266073,
-10.7442240979,
-10.8027285713,
-10.8363890521,
-10.8474912964,
-10.838157792,
-10.8103477586,
-10.7659387815,
-10.7066179666,
-10.6339907853,
-10.5495538639,
-10.4546677714,
-10.3506386542,
-10.2386366017,
-10.1197772808,
-9.99504030111,
-9.86535084973,
-9.73155247952,
];
julia> lsqfit(BirchMurnaghan3rd(40, 0.5, 4, 0)(Energy()), volumes, energies)
BirchMurnaghan3rd{Float64}(40.989265727925826, 0.5369258245608038, 4.1786442319302015, -10.842803908298968)
julia> lsqfit(Murnaghan(41, 0.5, 4, 0)(Energy()), volumes, energies)
Murnaghan{Float64}(41.13757924894751, 0.5144967655882123, 3.912386317519504, -10.836794511015869)
julia> lsqfit(PoirierTarantola3rd(41, 0.5, 4, 0)(Energy()), volumes, energies)
PoirierTarantola3rd{Float64}(40.86770643567383, 0.5667729960008705, 4.331688934942696, -10.851486685029547)
julia> lsqfit(Vinet(41, 0.5, 4, 0)(Energy()), volumes, energies)
Vinet{Float64}(40.91687567368755, 0.5493839427734198, 4.30519294991197, -10.846160810968053)
```

Then 4 different equations of state will be fitted.

## Public interfaces

`EquationsOfState.NonlinearFitting.lsqfit`

— Method`lsqfit(eos(prop), xdata, ydata; debug = false, kwargs...)`

Fit an equation of state using least-squares fitting method (with the Levenberg-Marquardt algorithm).

**Arguments**

`eos::EquationOfState`

: a trial equation of state. If it has units,`xdata`

and`ydata`

must also have.`prop::PhysicalProperty`

: a`PhysicalProperty`

instance. If`Energy`

, fit $E(V)$; if`Pressure`

, fit $P(V)$; if`BulkModulus`

, fit $B(V)$.`xdata::AbstractVector`

: a vector of volumes ($V$), with(out) units.`ydata::AbstractVector`

: a vector of energies ($E$), pressures ($P$), or bulk moduli ($B$), with(out) units. It must be consistent with`prop`

.`debug::Bool=false`

: if`true`

, then an`LsqFit.LsqFitResult`

is returned, containing estimated Jacobian, residuals, etc.; if`false`

, a fitted`EquationOfState`

is returned. The default value is`false`

.`kwargs`

: the rest keyword arguments are the same as that of`LsqFit.curve_fit`

. See its documentation and tutorial.