标签:nsf elseif fun global 线性 find atl orm upper
1.代码
%%列主元消去法
function ECPE = Elimination_of_column_pivot_entries(M,b)
global n;
[n,n] = size(M);
B =[M,b];
R_A = rank(M);R_B = rank(B);
if R_A ~= R_B
disp(‘方程无解‘);
elseif (R_A == R_B)&&(R_A == n)
disp(‘此方程有唯一解‘);
for k = 1:n-1
B = Column_pivot_transformation(B,k);
B = Elimination_method(B,k);
end
X = Upper_trig_iterative_solution(B);
else
disp(‘方程有无穷多组解‘);
end
disp(‘解向量为:‘);
ECPE = X;
%%列主元变换
%%指定p列,找出列最大值,以此值为主元进行行变换
function CPT = Column_pivot_transformation(M,p)
[m,n] = size(M);
s = max(M(p:m,p));
[x,y] = find(M(p:m,p) == s);
H = x+p-1;
Ch1 = M(H,:);
Ch2 = M(p,:);
M(H,:) = Ch2;
M(p,:) = Ch1;
CPT = M;
end
%%p列消元函数
function EM = Elimination_method(M,p)
[m,n] = size(M);Div = zeros(1,m);
for i = p+1:m
Div(i) = M(i,p)/M(p,p);
end
for j = p:n
for i = p:m
M(i,j) = M(i,j)-M(p,j)*Div(i);
end
end
EM = M;
end
%%上三角迭代法
function UTIS = Upper_trig_iterative_solution(M)
[m,n] = size(M);
A = M(:,1:n-1);ba = M(:,n);
x = zeros(1,m);
x(m) =ba(m)/A(m,m);
for i = m-1:-1:1
sum = 0;
for j = i+1:1:m
sum = sum+A(i,j)*x(j);
end
x(i) = (ba(i)-sum)/A(i,i);
end
UTIS = x‘;
end
end
2.例子
clear all clc M = rand(9) b = reshape(rand(3),9,1) S = Elimination_of_column_pivot_entries(M,b) M\b
结果
M =
列 1 至 7
0.2089 0.3502 0.8699 0.6473 0.4046 0.1389 0.7413
0.7093 0.6620 0.2648 0.5439 0.4484 0.6963 0.5201
0.2362 0.4162 0.3181 0.7210 0.3658 0.0938 0.3477
0.1194 0.8419 0.1192 0.5225 0.7635 0.5254 0.1500
0.6073 0.8329 0.9398 0.9937 0.6279 0.5303 0.5861
0.4501 0.2564 0.6456 0.2187 0.7720 0.8611 0.2621
0.4587 0.6135 0.4795 0.1058 0.9329 0.4849 0.0445
0.6619 0.5822 0.6393 0.1097 0.9727 0.3935 0.7549
0.7703 0.5407 0.5447 0.0636 0.1920 0.6714 0.2428
列 8 至 9
0.4424 0.3309
0.6878 0.4243
0.3592 0.2703
0.7363 0.1971
0.3947 0.8217
0.6834 0.4299
0.7040 0.8878
0.4423 0.3912
0.0196 0.7691
b =
0.3968
0.8085
0.7551
0.3774
0.2160
0.7904
0.9493
0.3276
0.6713
此方程有唯一解
解向量为:
S =
19.6917
7.6005
17.0314
-1.6699
-5.7675
-12.1059
-19.9661
10.5792
-18.0751
ans =
19.6917
7.6005
17.0314
-1.6699
-5.7675
-12.1059
-19.9661
10.5792
-18.0751
标签:nsf elseif fun global 线性 find atl orm upper
原文地址:https://www.cnblogs.com/guliangt/p/12119393.html