如何在mathematica中编写一个可以用Givens旋转求解线性方程组的程序。
代码尝试:
ar = CoefficientArrays[
{x + y + z == 2, 3 x - 2 y + z == 4, x - y + 5 z == 6},
{x, y, z}];
an = Normal[ar];
b = an[[2]];
n = Length[b];
Do[Do[a[i, j] = b[[i]][[j]], {j, 1, n}], {i, 1, n}];
r1 = (a[1, 1]^2 + a[1, 2]^2)^(1/2);
c = a[1, 1]/r1;
s = a[1, 2]/r1;
答案 0 :(得分:0)
你在找这样的东西吗?
关注您的代码。
{r1, c, s}
{Sqrt [2],1 / Sqrt [2],1 / Sqrt [2]}
{a, b} = LinearSolve[{{c, -s}, {s, c}}, {r1, 0}]
{1,-1}
GivensRotation[a_, b_] := Which[
b == 0, c = Sign[a]; s = 0; r = Abs[a],
a == 0, c = 0; s = -Sign[b]; r = Abs[b],
Abs[a] > Abs[b],
t = b/a; u = Sign[a]*Abs[Sqrt[1 + t*t]];
c = 1/u; s = -c*t; r = a*u,
True,
t = a/b;
u = Sign[b]*Abs[Sqrt[1 + t*t]];
s = -1/u; c = -s*t; r = b*u
]
GivensRotation[a, b];
{r, c, s}
{Sqrt [2],1 / Sqrt [2],1 / Sqrt [2]}
修改强>
我不熟悉用Givens旋转解决问题。以下是解决联立方程的其他方法,只是为了感兴趣。
Solve[{
x + y + z == 2,
3 x - 2 y + z == 4,
x - y + 5 z == 6},
{x, y, z}]
{{x - > 1,y - > 0,z - > 1}}
也
LinearSolve[{{1, 1, 1}, {3, -2, 1}, {1, -1, 5}}, {2, 4, 6}]
{1,0,1}}
或
Inverse[{{1, 1, 1}, {3, -2, 1}, {1, -1, 5}}].{2, 4, 6}
{1,0,1}}