求二次方和直线的交点 [英] Finding the intersect between a quadratic and line
问题描述
我试图找到一条直线和一条二次曲线之间的交点,但是我得到的结果似乎是虚构的,尽管我不明白这是怎么回事,因为我可以看到它们在实轴上相交:
I am trying to find the intersect between a straight line and a quadratic curve, however the result I am getting appears to be imaginary although I don't see how this can be the case as I can see them intersect on real axes:
Import numpy
#quadratic coefficients
a,b,c = (-3.09363812e-04, 1.52138019e+03, -1.87044961e+09)
# y = ax^2 + bx + c
#line coefficients
m,d = (1.06446434e-03, -2.61660911e+03)
#y = mx + d
intersect = (-(b-m)+((b-m)**2 - 4*a*(c-d))**0.5)/(2*a)
print(intersect)
这个的输出是2458883.4674943495-107.95731226786134j
The output of this is 2458883.4674943495-107.95731226786134j
我试图找到蓝色点上的黄色曲线和黑色虚线之间的交点
I am trying to find the intersect between the yellow curve over the blue points and the black dotted line
推荐答案
您呈现的图形曲线与您的方程不一样,并且您的方程不相交.
The graphed curves you presented vs. your equations are not the same, and your equations do not intersect.
我为您重写了一些示例代码.numpy
不是必需的,并且可能有一个确切的解决方案.
I rewrote some example code for you. numpy
isn't needed, and an exact solution is possible.
import math
import collections
def calculateIntersection(p, l):
b = p.B - l.slope
c = p.C - l.yInt
discriminant = b**2 - (4 * p.A * c)
if discriminant > 0.0:
# 2 points of intersection
x1 = (-b + math.sqrt(discriminant)) / (2.0 * p.A)
x2 = (-b - math.sqrt(discriminant)) / (2.0 * p.A)
return discriminant, [(x1, l.slope * x1 + l.yInt), (x2, l.slope * x2 + l.yInt)]
elif discriminant == 0.0:
# 1 point of intersection
x1 = -b / (2.0 * p.A)
return discriminant, [(x1, slope * x1 + l.yInt)]
else:
# no points of intersection
return discriminant, []
Line = collections.namedtuple('Line', 'slope yInt')
Poly = collections.namedtuple('Poly', 'A B C')
p = Poly(A=-3.09363812e-04, B=1.52138019e+03, C=-1.87044961e+09)
print(p)
l = Line(slope=1.06446434e-03, yInt=-2.61660911e+03)
print(l)
(discriminant, points) = calculateIntersection(p, l)
if (len(points) > 0):
print("Intersection: {}".format(points))
else:
print("No intersection: {}".format(discriminant))
这篇关于求二次方和直线的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!