intersection函数python,Python曲线使用numpy与fsolve()和函数参数相交

On order to find the intersection between two curves. Both curves basically are two arrays of floats.

The first of them is a one dimension array Pmech ( Pmech(x) ) and the second is a two dimension array Pair ( Pair(x,y) )

The x - axis is common for both arrays ,so what i want to do is for every y to see where Pair and Pmech intersect.

I am aware of the fact that fsolve() take as arguments functions, not arrays so I wrote two basic functions to implement this feature:

def Pmix(x):

return Pmech[x]

def Paera(x,y):

return Pair[x,y]

So as demonstrated in the above link I implemented the findIntersection function :

def findIntersection(fun1,fun2,x0):

return fsolve(lambda x: (fun1(x) - fun2(x,y) for y in range(1,100)),x0)

but I get the following error :

TypeError: float() argument must be a string or a number

Traceback (most recent call last):

File "batteries.py", line 261, in

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.

解决方案

It seems that in your example you might solve it much easier without fsolve:

import numpy as np

pair = np.array(pair)

pmech = np.array(pmech)

intersect_x=np.abs(pair-pmech[:,None]).argmin(0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值