# Fitting data with python

## Curve fitting

Preparing noisy data:

```Npoints = 30
x = np.linspace(1,10,100)
xb = np.linspace(1,10,Npoints)
f = lambda x: np.sin(x)
yb = f(xb) + 0.3*np.random.normal(size=len(xb))
```

Using a polynomial fit that is based on generalized linear regression algorithm, solving a linear system.

```from numpy.polynomial import polynomial as P
coeff, stats = P.polyfit(xb,yb,9,full=True)
fitpoly = P.Polynomial(coeff)
print stats
```

fitpoly is a function and coeff are the coefficients of the optimal polynomial.

Using curve-fit that calls *leastsq* algorithm, taking a step-by-step search for the minimum.

```fitfunc = lambda x, a, b: a*np.sin(b*x)
p, pcov = curve_fit(fitfunc,xb,yb,p0 = [1.0,1.0])
print p, np.sqrt(np.diag(pcov))
```

The last lines provides the found optimal parameters and their uncertainties. It is worth trying several guesses p0.

Plotting the results:

```import matplotlib.pyplot as plt
plt.scatter(xb,yb)
plt.plot(x,f(x))
plt.plot(x,fitpoly(x))
plt.plot(x,fitfunc(x,p,p))
plt.show()
```

## Using the least-square function directly

The basic syntax is the following:

```#!/usr/bin/python

from scipy import optimize
from numpy import array