执行LU分解而无需在MATLAB中进行透视 [英] Perform LU decomposition without pivoting in MATLAB

查看:90
本文介绍了执行LU分解而无需在MATLAB中进行透视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MATLAB中实现函数lu(A),以使L*U直接为A,同时获得真正的L矩阵?

How can I implement the function lu(A) in MATLAB so that L*U is directly A and I also get the real L matrix?

当我使用[L,U] = lu(A)时,MATLAB没有给我正确的L矩阵.当我使用[L,U,P] = lu(A)时,我需要实现P*A = L*U,但是我只想乘以L*U来接收A.

When I use [L,U] = lu(A), MATLAB doesn't give me the right L matrix. When I use [L,U,P] = lu(A), I need to implement P*A = L*U, but I only want to multiply L*U to receive A.

推荐答案

MATLAB的lu始终默认执行透视.例如,如果您尝试执行常规LU分解算法时对角系数等于0,则该对角系数将不起作用,因为在执行高斯消除以创建上三角矩阵U时需要对角系数,因此您将得到除以零的错误.需要旋转以确保分解稳定.

MATLAB's lu always performs pivoting by default. If you had for example a diagonal coefficient that was equal to 0 when you tried to do the conventional LU decomposition algorithm, it will not work as the diagonal coefficients are required when performing the Gaussian elimination to create the upper triangular matrix U so you would get a divide by zero error. Pivoting is required to ensure that the decomposition is stable.

但是,如果可以保证矩阵的对角线系数不为零,这非常简单,但是您必须自己编写.您要做的就是在矩阵上执行高斯消除,并将矩阵简化为简化的梯形形式.简化后的梯形形式矩阵为U,而在高斯消除中移除L的下三角部分所需的系数将放置在下三角部分中,以制成U.

However, if you can guarantee that the diagonal coefficients of your matrix are non-zero, it is very simple but you will have to write this on your own. All you have to do is perform Gaussian elimination on the matrix and reduce the matrix into reduced echelon form. The result reduced echelon form matrix is U while the coefficients required to remove the lower triangular part of L in Gaussian elimination would be placed in the lower triangular half to make U.

假设您的矩阵存储在A中,这样的事情可能会起作用.请记住,我在这里假设一个正方形矩阵.非旋转LU分解算法的实现放置在名为lu_nopivot:

Something like this could work, assuming your matrix is stored in A. Remember that I'm assuming a square matrix here. The implementation of the non-pivoting LU decomposition algorithm is placed in a MATLAB function file called lu_nopivot:

function [L, U] = lu_nopivot(A)

n = size(A, 1); % Obtain number of rows (should equal number of columns)
L = eye(n); % Start L off as identity and populate the lower triangular half slowly
for k = 1 : n
    % For each row k, access columns from k+1 to the end and divide by
    % the diagonal coefficient at A(k ,k)
    L(k + 1 : n, k) = A(k + 1 : n, k) / A(k, k);

    % For each row k+1 to the end, perform Gaussian elimination
    % In the end, A will contain U
    for l = k + 1 : n
        A(l, :) = A(l, :) - L(l, k) * A(k, :);
    end
end
U = A;

end

作为运行示例,假设我们具有以下3 x 3矩阵:

As a running example, suppose we have the following 3 x 3 matrix:

>> rng(123)
>> A = randi(10, 3, 3)

A =

     7     6    10
     3     8     7
     3     5     5

运行算法可以使我们:

>> [L,U] = lu_nopivot(A)

L =

    1.0000         0         0
    0.4286    1.0000         0
    0.4286    0.4474    1.0000   

U =

    7.0000    6.0000   10.0000
         0    5.4286    2.7143
         0         0   -0.5000

LU相乘得出:

>> L*U

ans =

     7     6    10
     3     8     7
     3     5     5

...是原始矩阵A.

这篇关于执行LU分解而无需在MATLAB中进行透视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆