程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> PLU decomposition Matlab version

PLU decomposition Matlab version

編輯:C++入門知識

PLU decomposition Matlab version


function [P, L, U] = plu(A)

% The implementation of PLU Factorization
% L is lower triangular and U is upper triangular
% P is permutation matrix

% Author: Zhenlin Du(Johnsondu)
% Email:  [email protected]
% Time:   2014-11-27 22:00

A = double(A);
[m, n] = size(A);
L1 = zeros(m, n);
L = zeros(m, min(m, n));
U1 = zeros(n, n);
U = zeros(min(m, n), n);
P = eye(m);

% row operation
for i = 1: m
	mval = 0.0;
	row = i;
	
	% find maximum number in current column
    for k = i : min(i, n)
        for j = i: m
            if abs(mval) < abs(A(j, k))
                mval = A(j, k);
                row = j;
            end
        end
    end
	
	% if current maximum number is zero
	% process the next column
	if mval == 0
		continue;
    end
	
	% exchange process, in P, L, U
	if row ~= i
		tmp = A(i, :);
		A(i, :) = A(row, :);
		A(row, :) = tmp;
		tmp = P(i, :);
		P(i, :) = P(row, :);
		P(row, :) = tmp;
		tmp = L1(i, :);
		L1(i, :) = L1(row, :);
		L1(row, :) = tmp;
	end
	
	for j = i+1 : m
		ratio = A(j, i) / mval;
		A(j, :) = A(j, :) - ratio * A(i, :);
		L1(j, i) = ratio;
	end	
end

for i = 1: min(m, n)
	L1(i, i) = 1.0;
end

for i = 1: m
    for j = 1: min(m, n)
        L(i, j) = L1(i, j);
    end
end

U1 = A;
for i = 1: min(m, n)
    for j = 1 : n
        U(i, j) = U1(i, j);
    end
end
	

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved