首页 > 解决方案 > 无旋转的带状矩阵版本的高斯消元法

问题描述

我是 Matlab 的新手,每当我编写代码时我都会面临挑战。我有一个关于没有旋转的高斯消除的 Matlab 代码,我想实现它的带状矩阵版本,它接受一个参数 k 表示每个方向上非零的非对角线条目的数量(带宽)那是,如果 i > j + k 或 j > i + k,则 aij = 0。函数可以像这样开始: 函数 x = BandedGauss(A,b,k)。 下面是我没有旋转的高斯消除:

function [x] = GE_WithoutPivoting1(A,b)
% Solve linear system Ax=b using Gaussian elimination

n = length(b);

for k=1:n-1

    % Check to see if the pivot is zero
    if abs(A(k,k)) < 1e-15
        error('A has diagonal entries of zero')
    end
    
    % Apply transformation to remaining submatrix and RHS vector
    for i=k+1:n
        m = -A(i,k)/A(k,k); % multiplier for current row i
        for j=k+1:n
            A(i,j) = A(i,j) + m*A(k,j);
        end
        b(i) = b(i) + m*b(k); 
    end
end

% A is now upper diagonal. Now, call the back substitution function to
% solve for the x solution vector i.e., the transformed problem:

x = GE_BackSubstitution(A,b);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [x] = GE_BackSubstitution(A,b)
% Solve the upper triangular system Ax=b using backward substitution

n= length(b);
x = zeros(n,1);

for j=n:-1:1    
    % Check to see if the diagonal entry is zero
    if abs(A(j,j)) < 1e-15
        error('A is singular (zero diagonal entry)')
    end
    
    % Compute solution component
    x(j) = b(j) / A(j,j);
    
    % Update the RHS vector
    for i=1:j-1
       b(i) = b(i) - A(i,j)*x(j);
    end
end

标签: matlabmatrixsparse-matrix

解决方案


推荐阅读