Python曲线使用numpy与fsolve()和函数参数相交 [英] Python curves intersection with fsolve() and function arguments using numpy

查看:388
本文介绍了Python曲线使用numpy与fsolve()和函数参数相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此处引用的fsolve:

I am trying to use fsolve as quoted here : http://glowingpython.blogspot.gr/2011/05/hot-to-find-intersection-of-two.html,

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

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

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

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

x-轴在两个数组中都是通用的,所以我要为每个y看到Pair和Pmech相交的地方.

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.

我知道fsolve()将函数而不是数组作为参数的事实,因此我编写了两个基本函数来实现此功能:

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]

因此,如上面的链接所示,我实现了findIntersection函数:

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)

但出现以下错误:

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.

推荐答案

似乎在您的示例中,如果没有fsolve,您可能会更轻松地解决它:

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)

这篇关于Python曲线使用numpy与fsolve()和函数参数相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆