[英]Exponential curve fit will not fit
When attempting to plot an exponential curve to a set of data: 尝试将指数曲线绘制到一组数据时:
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import style
from matplotlib import pylab
import numpy as np
from scipy.optimize import curve_fit
x = np.array([30,40,50,60])
y = np.array([0.027679854,0.055639098,0.114814815,0.240740741])
def exponenial_func(x, a, b, c):
return a*np.exp(-b*x)+c
popt, pcov = curve_fit(exponenial_func, x, y, p0=(1, 1e-6, 1))
xx = np.linspace(10,60,1000)
yy = exponenial_func(xx, *popt)
plt.plot(x,y,'o', xx, yy)
pylab.title('Exponential Fit')
ax = plt.gca()
fig = plt.gcf()
plt.xlabel(r'Temperature, C')
plt.ylabel(r'1/Time, $s^-$$^1$')
plt.show()
Graph for the above code: 上面的代码的图形:
However when I add the data point 20
(x) and 0.015162344
(y): 但是,当我添加数据点
20
(x)和0.015162344
(y)时:
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import style
from matplotlib import pylab
import numpy as np
from scipy.optimize import curve_fit
x = np.array([20,30,40,50,60])
y = np.array([0.015162344,0.027679854,0.055639098,0.114814815,0.240740741])
def exponenial_func(x, a, b, c):
return a*np.exp(-b*x)+c
popt, pcov = curve_fit(exponenial_func, x, y, p0=(1, 1e-6, 1))
xx = np.linspace(20,60,1000)
yy = exponenial_func(xx, *popt)
plt.plot(x,y,'o', xx, yy)
pylab.title('Exponential Fit')
ax = plt.gca()
fig = plt.gcf()
plt.xlabel(r'Temperature, C')
plt.ylabel(r'1/Time, $s^-$$^1$')
plt.show()
The above code generates the error 上面的代码生成错误
'RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 800.'
“ RuntimeError:找不到最佳参数:函数的调用次数已达到maxfev =800。”
If maxfev
is set to maxfev = 1300
如果将
maxfev
设置为maxfev = 1300
popt, pcov = curve_fit(exponenial_func, x, y, p0=(1, 1e-6, 1),maxfev=1300)
The graph is plotted but does not fit the curve correctly. 该图已绘制,但未正确拟合曲线。 Graph from above code change,
maxfev = 1300
: 来自上述代码更改的图形,
maxfev = 1300
:
I think this is because points 20 and 30 a too close to each other? 我认为这是因为点20和30 a彼此距离太近了吗? For comparison, excel plots the data like this:
为了进行比较,excel将数据绘制如下:
How can I plot this curve correctly? 如何正确绘制该曲线?
From your data it is obvious that you need a positive exponent, therefore, b
needs to be negative as you use a*np.exp(-b*x) + c
as the underlying model. 从数据中可以明显看出,您需要一个正指数,因此,当您使用
a*np.exp(-b*x) + c
作为基础模型时, b
必须为负。 However, you start with a positive initial value for b
which most likely causes the issues. 但是,您可以从
b
初始初始值开始,这很可能会引起问题。
If you change 如果你改变
popt, pcov = curve_fit(exponenial_func, x, y, p0=(1, 1e-6, 1))
to 至
popt, pcov = curve_fit(exponenial_func, x, y, p0=(1, -1e-6, 1))
it works fine and gives the expected outcome. 它工作正常,并给出了预期的结果。
Alternatively, you could also change your equation to 另外,您也可以将方程式更改为
return a*np.exp(b*x) + c
and start with the same initial values as you had. 并从与您相同的初始值开始。
Here is the entire code: 这是完整的代码:
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
def exponenial_func(x, a, b, c):
return a*np.exp(b*x)+c
x = np.array([20, 30, 40, 50, 60])
y = np.array([0.015162344, 0.027679854, 0.055639098, 0.114814815, 0.240740741])
popt, pcov = curve_fit(exponenial_func, x, y, p0=(1, 1e-6, 1))
xx = np.linspace(20, 60, 1000)
yy = exponenial_func(xx, *popt)
# please check whether that is correct
r2 = 1. - sum((exponenial_func(x, *popt) - y) ** 2) / sum((y - np.mean(y)) ** 2)
plt.plot(x, y, 'o', xx, yy)
plt.title('Exponential Fit')
plt.xlabel(r'Temperature, C')
plt.ylabel(r'1/Time, $s^-$$^1$')
plt.text(30, 0.15, "equation:\n{:.4f} exp({:.4f} x) + {:.4f}".format(*popt))
plt.text(30, 0.1, "R^2:\n {}".format(r2))
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.