高斯消除不起作用
Gaussian Elimination not working
我尝试编写一个使用高斯消去法进行 LU 分割的程序。我的程序将矩阵 A 拆分为 A=LR,其中 L、R 是三角矩阵。这非常有效。设 Ax=b 为方程组。我对该程序的输入是 (A,b),我希望将上三角矩阵 R 应用于 b 的操作就像我们在学校使用高斯消元法求解系统一样。那部分似乎不起作用。
有人能告诉我为什么它不起作用吗?
function [ x ] = Gauss( A,b )
n=length(b);
for j=1:n-1
if A(j,j)==0
break;
end;
for i=j+1:n
A(i,j)=A(i,j)/A(j,j);
b(j)=b(i)-A(i,j)*b(j)
for k=j+1:n
A(i,k)=A(i,k)-A(i,j)*A(j,k);
end
end
end
b
end
你说你的代码正确地创建了 A 的上三角矩阵,但它没有。让我举个例子。
设A和b为
A =
3 2 3 4
4 3 2 1
1 0 4 0
0 5 0 3
b =
2
4
6
7
如果我们运行你的代码,然后查看 A 和 b,我们得到
A =
3.0000 2.0000 3.0000 4.0000
1.3333 0.3333 -2.0000 -4.3333
0.3333 -2.0000 -1.0000 -10.0000
0 15.0000 -30.0000 -232.0000
b =
7.0000
-203.0000
187.0000
7.0000
这既不是三角矩阵,也不是我们期望的b。但是,如果我们稍微修改您的程序为:
function [ x ] = Gauss( A,b )
n=length(b);
for j=1:n-1
if A(j,j)==0
break;
end;
for i=j+1:n
f=A(i,j)/A(j,j); %%Save the proportion between the rows in a
%%different variable outside the matrix, or
%%you will loose the value that was originally there
b(i)=b(i)-f*b(j); %%The operation has to be done in the row you are currently working
for k=1:n %%You have to make the operation in the full row,
%%not only in the remaining columns, also you can
%%make this without a for loop using `:`
%%indexing, but if you dont know about it,
%%leave as it is, it works
A(i,k)=A(i,k)-f*A(j,k);
end
end
end
A
b
end
你得到这个结果
A =
3.0000 2.0000 3.0000 4.0000
0 0.3333 -2.0000 -4.3333
0 0 -1.0000 -10.0000
0 0 0 -232.0000
b =
2.0000
1.3333
8.0000
227.0000
其中是一个上三角矩阵,就是我们要的b。希望你可以从这里开始,作为参考,接下来的步骤应该是这样的
A =
1.0000 0.6667 1.0000 1.3333
0 1.0000 -6.0000 -13.0000
0 0 1.0000 10.0000
0 0 0 1.0000
b =
0.6667
4.0000
-8.0000
-0.9784
然后
A =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
b =
-1.1379
1.9871
1.7845
-0.9784
其中A已经是一个单位矩阵,这意味着b已经是我们的答案,我们可以证实做
A\b
ans=
-1.1379
1.9871
1.7845
-0.9784
我尝试编写一个使用高斯消去法进行 LU 分割的程序。我的程序将矩阵 A 拆分为 A=LR,其中 L、R 是三角矩阵。这非常有效。设 Ax=b 为方程组。我对该程序的输入是 (A,b),我希望将上三角矩阵 R 应用于 b 的操作就像我们在学校使用高斯消元法求解系统一样。那部分似乎不起作用。
有人能告诉我为什么它不起作用吗?
function [ x ] = Gauss( A,b )
n=length(b);
for j=1:n-1
if A(j,j)==0
break;
end;
for i=j+1:n
A(i,j)=A(i,j)/A(j,j);
b(j)=b(i)-A(i,j)*b(j)
for k=j+1:n
A(i,k)=A(i,k)-A(i,j)*A(j,k);
end
end
end
b
end
你说你的代码正确地创建了 A 的上三角矩阵,但它没有。让我举个例子。
设A和b为
A =
3 2 3 4
4 3 2 1
1 0 4 0
0 5 0 3
b =
2
4
6
7
如果我们运行你的代码,然后查看 A 和 b,我们得到
A =
3.0000 2.0000 3.0000 4.0000
1.3333 0.3333 -2.0000 -4.3333
0.3333 -2.0000 -1.0000 -10.0000
0 15.0000 -30.0000 -232.0000
b =
7.0000
-203.0000
187.0000
7.0000
这既不是三角矩阵,也不是我们期望的b。但是,如果我们稍微修改您的程序为:
function [ x ] = Gauss( A,b )
n=length(b);
for j=1:n-1
if A(j,j)==0
break;
end;
for i=j+1:n
f=A(i,j)/A(j,j); %%Save the proportion between the rows in a
%%different variable outside the matrix, or
%%you will loose the value that was originally there
b(i)=b(i)-f*b(j); %%The operation has to be done in the row you are currently working
for k=1:n %%You have to make the operation in the full row,
%%not only in the remaining columns, also you can
%%make this without a for loop using `:`
%%indexing, but if you dont know about it,
%%leave as it is, it works
A(i,k)=A(i,k)-f*A(j,k);
end
end
end
A
b
end
你得到这个结果
A =
3.0000 2.0000 3.0000 4.0000
0 0.3333 -2.0000 -4.3333
0 0 -1.0000 -10.0000
0 0 0 -232.0000
b =
2.0000
1.3333
8.0000
227.0000
其中是一个上三角矩阵,就是我们要的b。希望你可以从这里开始,作为参考,接下来的步骤应该是这样的
A =
1.0000 0.6667 1.0000 1.3333
0 1.0000 -6.0000 -13.0000
0 0 1.0000 10.0000
0 0 0 1.0000
b =
0.6667
4.0000
-8.0000
-0.9784
然后
A =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
b =
-1.1379
1.9871
1.7845
-0.9784
其中A已经是一个单位矩阵,这意味着b已经是我们的答案,我们可以证实做
A\b
ans=
-1.1379
1.9871
1.7845
-0.9784