Python使用numpy曲线与fsolve()和函数参数交叉

Dar*_*ine 6 python numpy intersection curves

我试图在这里引用fsolve:http://glowingpython.blogspot.gr/2011/05/hot-to-find-intersection-of-two.html,

为了找到两条曲线之间的交点.两条曲线基本上都是两个浮点数组.

第一个是一维数组Pmech ( Pmech(x) ),第二个是二维数组Pair ( Pair(x,y) )

x轴对于两个阵列都是通用的,所以我想要做的是每个y看到Pair和Pmech相交的位置.

我知道fsolve()作为参数函数,而不是数组,所以我写了两个基本函数来实现这个功能:

def Pmix(x):
    return Pmech[x]

def Paera(x,y):
    return Pair[x,y]
Run Code Online (Sandbox Code Playgroud)

所以我在上面的链接中演示了我实现的findIntersection功能:

def findIntersection(fun1,fun2,x0): 
    return fsolve(lambda x: (fun1(x) - fun2(x,y) for y in range(1,100)),x0)
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

TypeError: float() argument must be a string or a number
Traceback (most recent call last):
  File "batteries.py", line 261, in <module>
    findIntersection(Pmix,Paera,0)
  File "batteries.py", line 238, in findIntersection
    fsolve(lambda x: (fun1(x) - fun2(x,y) for y in range(1,100) ),x0)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 125, in fsolve
    maxfev, ml, mu, epsfcn, factor, diag)
minpack.error: Result from function call is not a proper array of floats.
Run Code Online (Sandbox Code Playgroud)

HYR*_*YRY 3

from scipy.optimize import fsolve

def pmix(x):
    return x

def paera(x, y):
    return x**2 - y**2

def findIntersection(fun1, fun2, x0):
    return [fsolve(lambda x:fun1(x)-fun2(x, y), x0) for y in range(1, 10)]

print findIntersection(pmix, paera, 0)
Run Code Online (Sandbox Code Playgroud)