找到曲线上的单个点(指数函数)

时间:2014-03-28 13:49:54

标签: python function matplotlib exponential

我有一个指数函数,用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)

1 个答案:

答案 0 :(得分:0)

您可以使用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])