在曲线上寻找单个点(指数函数)

2024-04-20 08:09:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个指数函数,用matplotlib画一条曲线。问题是:我想得到v的值,其中la=4.0。我怎么得到它?你知道吗

v = np.array([1.1,2.6,3.1,4.9,5.4,6.0])  
la = np.array([1,4,8,9,10,20])  

def func(x, a, b):
        return a*np.exp(b*x)

popt, pcov = curve_fit(func, v, la)
yn = func(v, *popt)
ax = self.figure.add_subplot(111)
l = ax1.plot(v, yn, 'rx')
ax.plot(v, yn)

Tags: returnplotmatplotlibdefnpaxarray曲线
1条回答
网友
1楼 · 发布于 2024-04-20 08:09:50

您可以使用fsolve来查找具有特定值的x坐标,在您的例子中是4。这是一个正确的例子:

from scipy.optimize import curve_fit,fsolve

v = np.array([1.1,2.6,3.1,4.9,5.4,6.0])  
la = np.array([1,4,8,9,10,20])  

def func(x, a, b):
        return a*np.exp(b*x)

popt, pcov = curve_fit(func, v, la)
yn = func(v, *popt)
fig,ax1 = plt.subplots()
l = ax1.plot(v, la, 'rx')
ax1.plot(v, func(v,*popt))


#exact match, as the value 4 appears in your original data
index = where(la==4)[0]
vla4exact = v[index]#array([ 2.6])

#by finding the zero of the fit curve. This works for any value
x0=2 #choose this initial guess for fsolve with care
vla4fsolve = fsolve(lambda x: func(x,*popt)-4,x0)#array([ 2.6])

相关问题 更多 >