[Tutor] fit data to equation
Oscar Benjamin
oscar.j.benjamin at gmail.com
Fri Sep 6 16:16:18 CEST 2013
On 6 September 2013 13:19, I. Alejandro Fleischer <iafleischer at gmail.com> wrote:
>
> It's a variation , of a physical value ("y") in time ("x") (while
> cooling) , you have the data measured (xi, yi), but not from x=0. I need to
> extrapolate "y" to "x=0", by that equation.
>
> I know the very basics about statistics, and a beginner in python, I ve
> chosen python to work with.
Okay well you'll want numpy, scipy and matplotlib if you don't have
those. Most documentation for those assumes familiarity with Python so
you may want to work through a bit of a python tutorial first (if you
haven't already). Then I'd suggest first writing a script that can
just plot your data-points using matplotlib.
Then for your actual fitting problem you'll want to use leastsq from
scipy's optimize module. See section 5.4 in this link:
http://www.tau.ac.il/~kineret/amit/scipy_tutorial/
Here's a basic example of how to use the function:
from scipy.optimize import leastsq
# The "true values"
amin = 2
bmin = 3
# The residual as a function of the parameters a and b
def residual(ab):
a, b = ab
return [amin - a, bmin - b]
# Initial estimate of parameters a and b
aguess = 0
bguess = 0
# Fitted values from fitting algorithm
(afit, bfit), _ = leastsq(residual, (aguess, bguess))
print('afit: %s' % afit)
print('bfit: %s' % bfit)
Oscar
More information about the Tutor
mailing list