LU矩阵求解

  • 本文关键字:LU matlab
  • 更新时间 :
  • 英文 :


这是我目前使用旋转进行自己的LU分解的尝试。它似乎适用于2x2和3x3矩阵,但没有更大的。在对内置的lu进行测试时,我似乎找不到出了什么问题。

function [L, U, P] = luFactor(A)
U = A;          %   U = upper triangular matrix
n = size(A);    %   n = number of rows in A
L = eye(n);     %   L = lower triangular matrix
P = eye(n);     %   P = initial pivot matrix
[n m]=size(A);
if n ~= m          %check if A is square
error('Check dimensions of A')
end
for j=1:n-1
[~,ind] = max(abs(U(j:n,j)));   %find what row largest value is in
maxrow = U(ind+(j-1),1:n);      %save that rows that will be swapped
swaprow = U(j,1:n);
U(j,1:n)=maxrow;
U(ind+(j-1),1:n)=swaprow;
maxp = P(ind+(j-1),:);          %pivot P matrix to follow U
swapp = P(j,:);
P(j,:) = maxp;
P(ind+(j-1),:) = swapp;
for i=j:n-1
L(i+1,j) = U(i+1,j)/U(j,j)          %determine coefficient to eliminate a variable
U(i+1,:) = -(U(i+1,j)/U(j,j))*U(j,:)+U(i+1,:)      %multiply coeff by first row then subtract
end
end
end

我意识到我的L矩阵没有旋转

相关内容

  • 没有找到相关文章

最新更新