PA = LU分解Matlab

时间:2019-10-26 23:24:00

标签: matlab gaussian factorization

目标是在Matlab中实现此伪代码

输入:A是一个n x m矩阵,m> = n
输出:PA = LU分解存储在一个矩阵和一个向量中

 STEP 1: Set P = (1:n)', set M = A

 STEP 2: For i = 1,2,...,n-1 do STEPS 3-6 

 STEP 3: Find k so that abs(M(k,i)) >= abs(M(l,i)) for l = i,i+1,...,n

 STEP 4: Interchange rows i and rows k of M and entries i and k of P

 STEP 5: If M(i,i) = 0
         OUTPUT("Matrix is rank deficient")

 STEP 6: For j = i+1,i+2,...,n do STEPS 7-9

 STEP 7: Set  a = M(j,i)/M(i,i)

 STEP 8: Set  M(j,i:end) = M(j,i:end) - a*M(i,i:end)

 STEP 9: Set  M(j,i) = a

 STEP 10: OUTPUT([P,M]); STOP.

在STEP 6的最后,对于每个i,您都有一个矩阵[P(i),A(i)]
构造矩阵

这是我编写的代码:

clear all; close all;
r = RandStream('mt19937ar','Seed',1234);
A = r.randn(6,6);

out = [];
P= (1:6)';
M=A;
for i=1:5
    for l=i:6
        for k=l+1:6
            if abs(M(k,i)) >= abs(M(l,i))
                final_K=k;
                %interchange rows and entries
                temp=M(i,:);
                M(final_K,:)=M(i,:);
                M(i,:)=temp;

                temp_2=P(1,i);
                P(1,final_K)=P(1,i);
                P(1,i)=temp_2; 
            end
        end    

    end
    if M(i,i) == 0
       disp('Matrix is rank deficient');
    end 

    for j=i+1:6
        a=M(j,i)/M(i,i);
        M(j,i:end) = M(j,i:end) - a*M(i,i:end);
        M(j,i)=a;
        out = [P,M];
    end

end

我希望输出为30 * 7矩阵

0 个答案:

没有答案