python画函数求交点,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:

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

解决方案

It's similar to:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值