Examples
Example 1:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Define the function to be fitted
def func(x,a, b, c):
return a * np.exp(-b * x) + c
# Generate some synthetic data
x_data = np.linspace(0, 10, 100)
y_data = func(x_data, 2, 0.5, 1)
y_data += 0.5 * np.random.normal(size=len(x_data))
# Perform the curve fitting
popt, pcov = curve_fit(func, x_data, y_data)
# Print the fitted parameters
print("Fitted parameters:", popt)
# Plot the data and the fitted curve
plt.scatter(x_data, y_data, label='data')
plt.plot(x_data, func(x_data, *popt), 'r-', label='fit')
plt.legend()
plt.show()