用matlab分别编写程序,用以下方法求解上述方程 (1)Gauss消元法; (2)列主元Gauss消元法; (3)Doolittle分解法; (4)列主元Doolittle分解法 (5)Crout分解法.
时间: 2024-05-24 18:14:44 浏览: 103
好的,我明白了。以下是针对您的问题的答案:
1. Gauss消元法的matlab程序:
function [x] = Gauss_elimination(A, b)
n = size(A, 1);
aug = [A, b];
for i = 1:n-1
[pivot, row] = max(abs(aug(i:n, i)));
row = row + i - 1;
if pivot == 0
error('无解或有无数解');
end
if row ~= i
aug([i, row], :) = aug([row, i], :);
end
for j = i+1:n
factor = aug(j, i) / aug(i, i);
aug(j, :) = aug(j, :) - factor * aug(i, :);
end
end
if aug(n, n) == 0
error('无解或有无数解');
end
x = zeros(n, 1);
x(n) = aug(n, n+1) / aug(n, n);
for i = n-1:-1:1
x(i) = (aug(i, n+1) - aug(i, i+1:n) * x(i+1:n)) / aug(i, i);
end
end
2. 列主元Gauss消元法的matlab程序:
function [x] = Gauss_elimination_col_pivoting(A, b)
n = size(A, 1);
ind = 1:n;
for i = 1:n-1
[pivot, row] = max(abs(A(i:n, i)));
row = row + i - 1;
if pivot == 0
error('无解或有无数解');
end
if row ~= i
ind([i, row]) = ind([row, i]);
A([i, row], :) = A([row, i], :);
b([i, row]) = b([row, i]);
end
for j = i+1:n
factor = A(j, i) / A(i, i);
A(j, :) = A(j, :) - factor * A(i, :);
b(j) = b(j) - factor * b(i);
end
end
if A(n, n) == 0
error('无解或有无数解');
end
x = zeros(n, 1);
x(n) = b(n) / A(n, n);
for i = n-1:-1:1
x(i) = (b(i) - A(i, i+1:n) * x(i+1:n)) / A(i, i);
end
x(ind) = x;
end
3. Doolittle分解法的matlab程序:
function [L, U] = Doolittle(A)
n = size(A, 1);
L = eye(n);
U = zeros(n);
for i = 1:n
U(i, i) = A(i, i) - L(i, 1:i-1) * U(1:i-1, i);
for j = i+1:n
L(j, i) = (A(j, i) - L(j, 1:i-1) * U(1:i-1, i)) / U(i, i);
U(i, j) = A(i, j) - L(i, 1:i-1) * U(1:i-1, j);
end
end
end
4. 列主元Doolittle分解法的matlab程序:
function [L, U] = Doolittle_col_pivoting(A)
n = size(A, 1);
ind = 1:n;
L = eye(n);
U = zeros(n);
for i = 1:n
[pivot, row] = max(abs(A(i:n, i)));
row = row + i - 1;
if pivot == 0
error('无法进行LU分解');
end
if row ~= i
ind([i, row]) = ind([row, i]);
A([i, row], :) = A([row, i], :);
L([i, row], 1:i-1) = L([row, i], 1:i-1);
end
U(i, i) = A(i, i) - L(i, 1:i-1) * U(1:i-1, i);
for j = i+1:n
L(j, i) = (A(j, i) - L(j, 1:i-1) * U(1:i-1, i)) / U(i, i);
U(i, j) = A(i, j) - L(i, 1:i-1) * U(1:i-1, j);
end
end
L(ind, :) = L;
end
5. Crout分解法的matlab程序:
function [L, U] = Crout(A)
n = size(A, 1);
L = zeros(n);
U = eye(n);
for i = 1:n
L(i, i) = A(i, i) - L(i, 1:i-1) * U(1:i-1, i);
for j = i+1:n
L(j, i) = (A(j, i) - L(j, 1:i-1) * U(1:i-1, i)) / U(i, i);
U(i, j) = A(i, j) - L(i, 1:i-1) * U(1:i-1, j);
end
end
end
希望以上程序能够对您有所帮助。
阅读全文