In [1]:
%matplotlib inline
import numpy as np
from matplotlib import pylab
from curve_fit import *
In [2]:
def linear_fit(x, a, b):
return a*x + b
In [3]:
x = np.arange(0, 1000, 1)
y = 1.5*x+10 + np.random.uniform(-100, 100, len(x))
In [4]:
pylab.plot(x, y, '.')
Out[4]:
In [13]:
parameters = Parameters([Parameter("a", 1), Parameter("b", 0)])
data = SimpleFitInputData(x, y, id="Example Fit")
fit_performer = SimpleMinimizeFit(data, parameters, fit_func=linear_fit)
The perform_fit
method does fit and returns a fitted function.
In [18]:
fitted_func = fit_performer.perform_fit()
Parameter values are accessible using:
In [20]:
fit_performer.fit_result.params.param_values
Out[20]:
In [21]:
pylab.plot(x, y, '.')
pylab.plot(fit_performer.fit_result.plot_geometry, fit_performer.fit_result.plot_profile, linewidth=3)
Out[21]:
Let's assume that we know that in this particular case "true" value of parameter b is 10. In this case we still pass "b" parameter
to the fitter, but we mark it as disabled --- it will still be passed to linear_fit
but will not take part in fitting at all.
In [22]:
parameters = Parameters([Parameter("a", 1), Parameter("b", 10, enabled=False)])
data = SimpleFitInputData(x, y, id="Example Fit")
fit_performer = SimpleMinimizeFit(data, parameters, fit_func=linear_fit)
In [23]:
fit_performer.perform_fit()
Out[23]:
In [24]:
fit_performer.fit_result.params.param_values
Out[24]:
In [27]:
parameters = Parameters([Parameter("a", 1, min_bound=0), Parameter("b", 10, min_bound=5, max_bound=15)])
data = SimpleFitInputData(x, y, id="Example Fit")
fit_performer = SimpleMinimizeFit(data, parameters, fit_func=linear_fit)
In [28]:
fitted_func = fit_performer.perform_fit()
In [29]:
fit_performer.fit_result.params.param_values
Out[29]:
In [30]:
pylab.plot(x, y, '.')
pylab.plot(fit_performer.fit_result.plot_geometry, fit_performer.fit_result.plot_profile, linewidth=3)
Out[30]:
In [ ]: