Python - 找到2个图的所有交点

标签 python numpy matplotlib scipy

我试图找到两个图的所有交点并将它们显示在最终图上。我环顾四周并尝试了多种方法,但无法获得我正在寻找的东西。

目前,我试图生成一个列表,其中将列出交点,但我不断收到以下错误:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

import numpy as np
from scipy.optimize import fsolve
import matplotlib.pyplot as plt


x = np.arange(-7.0, 7.0, 0.05)

def y(x):
    return np.sin(x)*(0.003*x**4 - 0.1*x**3 + x**2 + 4*x + 3)

def g(x):
    return -10 * np.arctan(x)

def intersection(x):
    if (y(x) - g(x)) == 0:
        print y.all(x)

plt.plot(x, y(x), '-')
plt.plot(x, g(x), '-')

plt.show()

最佳答案

类似于:

Intersection of two graphs in Python, find the x value:

import numpy as np
from scipy.optimize import fsolve
import matplotlib.pyplot as plt


x = np.arange(-7.0, 7.0, 0.05)

y = np.sin(x)*(0.003*x**4 - 0.1*x**3 + x**2 + 4*x + 3)

g = -10 * np.arctan(x)

def intersection():
    idx = np.argwhere(np.isclose(y, g, atol=10)).reshape(-1)
    print idx

    plt.plot(x, y, '-')
    plt.plot(x, g, '-')

    plt.show()

intersection()

编辑:您不使用函数,而是使用值列表

关于Python - 找到2个图的所有交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31541080/

相关文章:

python - 如何在 python lark 解析器中平衡规则和终端?

c++ - 为什么 Armadillo 的 SVD 结果与 NumPy 不同?

python - 初始化一个numpy数组

python - 使用 numpy 在 python 中执行 varimax 旋转

Python:根据类的无效 RGBA 参数 0.0 色点

python - Matplotlib Legend for Scatter 自定义颜色

python - 如何提取洗衣机前面板的轮廓?

python - Django:Python 全局变量重叠,即使是单独运行

python - 如何将 QComboBox 放入 QColumnView 中

python - LinAlg错误: SVD did not converge in matplotlib PCA