Finding the intersect between a quadratic and line

*爱你&永不变心* 提交于 2021-01-29 10:30:55

问题


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)

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


回答1:


The graphed curves you presented vs. your equations are not the same, and your equations do not intersect.

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


来源:https://stackoverflow.com/questions/60832850/finding-the-intersect-between-a-quadratic-and-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!