Skip to main content
 首页 » 编程设计

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

2025年05月04日15JustinYoung

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

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

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() 

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