解决到code进行联立方程 [英] Solving a simultaneous equation through code
问题描述
这似乎是一个令人难以置信的简单和愚蠢的问题要问,但一切我发现它已经太复杂,我明白了。
我有两个非常基本的联立方程:
X = 2X + 2Z
Y = Z - X
既然我知道X和Y,我将如何去寻找X和Z?这很容易做手工,但我不知道如何做到这一点的code来完成。
这似乎是一个令人难以置信的简单和愚蠢的问题要问。
不尽然。这是一个很好的问题,而且它有不幸的是一个复杂的答案。让我们来解决。
A * X + B * Y = U
C * X + D *ÿ= V
我坚持到2x2这里的情况。更复杂的情况下,将要求您使用的库。
要注意的第一件事是,克拉默公式不是很好用。当你计算行列式
A * D - B * C
只要你有 A * D〜B * C
,那么你的灾难性取消。这起案件是典型的,而你的必须的警惕了。
简单/稳定性之间的最佳折衷是的部分回转。假设 | A | > | C |
。然后该系统相当于
A * C / A * X + BC / A * Y = UC / A
C * X + D *ÿ= V
这是
CX + BC / A * Y = UC / A
CX + DY = V
和现在,减去第一到第二产量
CX + BC / A * Y = UC / A
(D - BC / A)* Y = V - UC / A
这是现在简单的解决: Y =(V - UC / A)/(D - BC / A)
和 X =( UC / A - BC / A * Y)/ C
。计算 D - BC / A
更稳定比广告 - BC
,因为我们除以数量最多(它不是很明显,但它拥有 - 做计算具有非常密切的系数,你就会明白为什么它的工作原理)
现在,如果 | C | > | A |
,你只是换行,并继续同样
在code(请检查Python语法):
高清解决(A,B,C,D,U,V):
如果绝对(a)及GT; ABS(三):
F = U * C / A
G = B * C / A
Y =(V - F)/(D - G)
返程((F - G * Y)/ C,Y)
其他
F = V *的A / C
G = D * A / C
X =(U - 六)/(B - G)
返回(X,(F - G * X)/ A)
可以使用全部枢轴旋转(需要您交换x和y,使得第一除法总是由最大的系数),但这是比较麻烦写,和几乎从未所需的2×2的情况
对于N×N的情况下,所有旋转的东西被封装到 LU分解和你应该使用库这一点。
This seems like an incredibly simple and silly question to ask, but everything I've found about it has been too complex for me to understand.
I have these two very basic simultaneous equations:
X = 2x + 2z
Y = z - x
Given that I know both X and Y, how would I go about finding x and z? It's very easy to do it by hand, but I have no idea how this would be done in code.
This seems like an incredibly simple and silly question to ask
Not at all. This is a very good question, and it has unfortunately a complex answer. Let's solve
a * x + b * y = u
c * x + d * y = v
I stick to the 2x2 case here. More complex cases will require you to use a library.
The first thing to note is that Cramer formulas are not good to use. When you compute the determinant
a * d - b * c
as soon as you have a * d ~ b * c
, then you have catastrophic cancellation. This case is typical, and you must guard against it.
The best tradeoff between simplicity / stability is partial pivoting. Suppose that |a| > |c|
. Then the system is equivalent to
a * c/a * x + bc/a * y = uc/a
c * x + d * y = v
which is
cx + bc/a * y = uc/a
cx + dy = v
and now, substracting the first to the second yields
cx + bc/a * y = uc/a
(d - bc/a) * y = v - uc/a
which is now straightforward to solve: y = (v - uc/a) / (d - bc/a)
and x = (uc/a - bc/a * y) / c
. Computing d - bc/a
is stabler than ad - bc
, because we divide by the biggest number (it is not very obvious, but it holds -- do the computation with very close coefficients, you'll see why it works).
Now, if |c| > |a|
, you just swap the rows and proceed similarly.
In code (please check the Python syntax):
def solve(a, b, c, d, u, v):
if abs(a) > abs(c):
f = u * c / a
g = b * c / a
y = (v - f) / (d - g)
return ((f - g * y) / c, y)
else
f = v * a / c
g = d * a / c
x = (u - f) / (b - g)
return (x, (f - g * x) / a)
You can use full pivoting (requires you to swap x and y so that the first division is always by the largest coefficient), but this is more cumbersome to write, and almost never required for the 2x2 case.
For the n x n case, all the pivoting stuff is encapsulated into the LU decomposition, and you should use a library for this.
这篇关于解决到code进行联立方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!