在Python/Numpy/Scipy中找到两个数组之间的插值交点 [英] finding the interpolated intersection between two arrays in Python/Numpy/Scipy

查看:432
本文介绍了在Python/Numpy/Scipy中找到两个数组之间的插值交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的方法来查找两个Numpy数组之间的插值交点.我知道如果我们有两个函数句柄而不是两个数组,就可以轻松实现这一点,如

I am looking for a simple approach to finding the interpolated intersection between two Numpy arrays. I know that this is simply achieved if we have two function handles, rather than two arrays, as shown in this link using Scipy or using Sympy. I want to do the same, but given two arrays, specifically between the linear spline that results from connecting array entries by lines.

例如,假设我们有两个数组y_1y_2,都认为它们是在xSupport处求值的.

For example, suppose we have two arrays, y_1 and y_2, both thyought of as being evaluated at xSupport.

import numpy as np
xSupport = np.array([0,1])
y_1 = np.array([0,2])
y_2 = np.array([1,0])

我正在寻找返回1/3的函数,该函数是这两条线之间的交点处的x值.在我的应用程序中,支持大于两个,因此我正在寻找一种与数组长度无关的方法.

I am looking for the function that returns 1/3, which is the x value at the intersections between these two lines. In my application the support is larger than two, so I am looking for an approach that is independent of the arrays' length.

推荐答案

与ser的回答相同:

import numpy as np
x = np.array([0,1])
y1 = np.array([0,2])
y2 = np.array([1,0])

def solve(f,x):
    s = np.sign(f)
    z = np.where(s == 0)[0]
    if z:
        return z
    else:
        s = s[0:-1] + s[1:]
        z = np.where(s == 0)[0]
        return z

def interp(f,x,z):
    m = (f[z+1] - f[z]) / (x[z+1] - x[z])
    return x[z] - f[z]/m

f = y1-y2
z = solve(f,x)
ans = interp(f,x,z)

print(ans)

可以通过假设找到一个零,然后对两个序列的差执行函数来简化该问题.首先,求解"查找发生符号过渡的位置(这意味着在两者之间的某个位置出现零),然后插入"执行线性插值以找到解决方案.

The problem can be simplified by assuming that you're finding a zero, and then performing the function on the difference of the two series. First, 'solve' finds where a sign transition occurs (implying a zero occurs somewhere in between) and then 'interp' performs a linear interpolation to find the solution.

这篇关于在Python/Numpy/Scipy中找到两个数组之间的插值交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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