Python-查找2个图的所有交点 [英] Python - Find all intersection points of 2 graphs
本文介绍了Python-查找2个图的所有交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试查找两个图形的所有交点并将其显示在最终绘图上.我环顾四周并尝试了多种方法,但无法获得我想要的东西.
I'm trying to find all the intersection points of two graphs and display them on the final plot. I've looked around and tried multiple things, but I haven't been able to obtain what l'm looking for.
当前,我一直试图生成一个列表,其中将列出相交点,尽管我不断遇到以下错误:
Currently, I attempting to generate a list wherein the intersection points would be listed, though I keep getting the following error:
具有多个元素的数组的真值是不明确的. 使用
a.any()
或a.all()
.
The truth value of an array with more than one element is ambiguous. Use
a.any()
ora.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()
推荐答案
它类似于:
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()
您不使用函数,而是使用值列表
edit: you don't use a function, but a list of values
这篇关于Python-查找2个图的所有交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文