2014-03-28 13 views
2

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

回答

0

您可以使用fsolve地发现,具有一定的价值,你的情况4x坐标。这是一个更正的例子:

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])