I have
x_data = [3, 6, 9, 12, 16, 21, 28, 50]
and
y_data = [0, 333.33333333, 333.33333333, 333.33333333, 250, 200, 142.85714286, 45.45454545]
I have attached a graph of what this looks like:
I need to fit this to a curve with function: f(x) = a * exp(-x / b) + c.
However I can't get scipy curve_fit
to work. How can I go about this?
You have to define the function you want to fit and then pass it to curve_fit
. Your function must take the independent variable first and all the remaining parameters after that.
def func(x, a, b, c):
return a * np.exp(-x / b) + c
# Get parameters and covariance
params, cov = scipy.optimize.curve_fit(f=f, xdata=x_data, ydata=y_data)
# Declare dedicated letters for each parameter
a, b, c = params
# Plot
plt.plot(x_data, y_data)
plt.plot(x_data, [func(x, a=a, b=b, c=c) for x in x_data])
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.