December 2017
Intermediate to advanced
386 pages
10h 42m
English
The following code represents the functions of the executed equations:
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit
def func(x, a, b, c): ... return a * np.exp(-b * x) + c
xdata = np.linspace(0, 4, 50) y = func(xdata, 2.5, 1.3, 0.5) y_noise = 0.2 * np.random.normal(size=xdata.size) ydata = y + y_noise plt.plot(xdata, ydata, 'b-', label='data')
popt, pcov = curve_fit(func, xdata, ydata) plt.plot(xdata, func(xdata, *popt), 'r-', label='fit')
popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 2., 1.])) plt.plot(xdata, func(xdata, *popt), 'g--', label='fit-with-bounds')
plt.xlabel('x') plt.ylabel('y') plt.legend() plt.show()
The following is the final result:
Read now
Unlock full access