我需要找到一个函数的根源。我目前正在使用 scipy.optimize.fsolve
def f(z):
return ((1-2*z)*np.exp(-d/z))/(((1-z)**(2+d))*(z**(2-d)))
# d is a constant ndarray of shape(300000,)
# hence f(z) is an ndarray of shape (300000,)
def trap(func,a,b,num): #trapezoidal approach to integration
xlinear=np.linspace(a,b,num)
dx=np.diff(xlinear)
fx=func(xlinear[:,None])
f12=fx[:-1,:]+fx[1:,:]
return np.dot(dx,f12)/2.0
#also returns an array of shape (300000,)
def integral(p):
return trap(f,0,p,1000)
root= fsolve(f,0.75) # 0.75 because the answer should lie in (0.5,1)
问题是我期望 fsolve 返回一个与函数 f 和陷阱的返回一致的数组。但是,不知何故,我只得到了一个形状数组 (1,)