我试图找到两个图的所有交点并将它们显示在最终的绘图上。我环顾四周并尝试了多种方法,但一直无法获得我想要的东西。
目前,我尝试生成一个列表,其中将列出交点,但我不断收到以下错误:
具有多个元素的数组的真值是不明确的。
使用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()
它类似于:
Python 中两个图的交集,求 x 值: https://stackoverflow.com/questions/28766692/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()
编辑:您不使用函数,而是使用值列表
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)