I have an exponential function, plot a curve with matplotlib. Problem is: I want to have the value of v where la=4.0. How do I get it?

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)
有帮助吗?

解决方案

You can use fsolve to find the x coordinates that has a certain value, in your case 4. This is a corrected example:

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])
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top