In [ ]:
%matplotlib inline
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
In [ ]:
def f(x, a, b):
return a * x + b
x = np.arange(-3., 3., 0.1)
y = f(x, 1., 2.)
yn = y + 0.9 * np.random.normal(size=x.shape[0])
popt, pcov = curve_fit(f, x, yn)
print(popt)
print(pcov)
In [ ]:
plt.title('Fitting Example')
plt.plot(x, y,'g.--')
plt.plot(x, yn,'k.')
plt.plot(x, f(x, *popt),'r.-')
plt.legend(['original','plus noise', 'fit'])
plt.show()
In [ ]: