gpt4 book ai didi

algorithm - 求两个线性方程交集的算法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:31:03 24 4
gpt4 key购买 nike

我正在努力找出一种算法来找到两个线性方程的交集,例如:

f(x)=2x+4

g(x)=x+2

我想用你设f(x)=g(x)的方法求解x,我想远离叉积。

有人对这样的算法有什么建议吗?

最佳答案

如果您的输入线是斜截线形式,那么算法就大材小用了,因为有一个直接公式可以计算它们的交点。它在 Wikipedia page 上给出你可以按照下面的解释来理解它。

Given the equations of the lines: The x and y coordinates of the point of intersection of two non-vertical lines can easily be found using the following substitutions and rearrangements.

Suppose that two lines have the equations y = ax + c and y = bx + d where a and b are the slopes (gradients) of the lines and where c and d are the y-intercepts of the lines. At the point where the two lines intersect (if they do), both y coordinates will be the same, hence the following equality:

ax + c = bx + d.

We can rearrange this expression in order to extract the value of x,

ax - bx = d - c, and so,

x = (d-c)/(a-b).

To find the y coordinate, all we need to do is substitute the value of x into > either one of the two line equations. For example, into the first:

y=(a*(d-c)/(a-b))+c.

Hence, the Point of Intersection is {(d-c)/(a-b), (a*(d-c)/(a-b))+c}

Note: If a = b then the two lines are parallel. If c ≠ d as well, the lines are different and there is no intersection, otherwise the two lines are identical.

关于algorithm - 求两个线性方程交集的算法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32702724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com