LU decomposition code, don't know what it's doing. Can someone explain what this code is doing line-by-line?

4 ビュー (過去 30 日間)
Hi,
I'm trying to modify this code that performs an LU decompositon for a matrix A via column operations. The default code is written for row operations.
I don't understand what the code is doing though for the row operations. Does anyone mind explaining what is going on line-by-line? I know how to do LU decomposition by hand, but I don't have a really good idea what the script is doing.
I'll include the programme for you.
Thank You!
function [L,U] = mylu(A)
n = size(A,1);
for k = 1:n
if A(k,k)==0
warning('LU factorization fails');
L = []; U = []; return;
end
i = k+1:n;
A(i,k) = A(i,k)/A(k,k);
A(i,i) = A(i,i)-A(i,k)*A(k,i);
end
L = tril(A,-1)+eye(n); U = triu(A);

採用された回答

Harsha Priya Daggubati
Harsha Priya Daggubati 2020 年 1 月 28 日
編集済み: Harsha Priya Daggubati 2020 年 1 月 28 日
Hi, In LU Decomposition method we try to convert A matrix to echleon form by using gauss elimination method. The code starts from the first row, tries to find the factor by which we need to multiply the current row and subtract it from the rows below it, to make the elements in the Lower Triangular Matrix as zeros.
i = k+1:n; %To access rows below the current row
A(i,k) = A(i,k)/A(k,k); % To get the factor by which we need to multiply the current row and subtract it from row present below
A(i,i) = A(i,i)-A(i,k)*A(k,i); % To subtract from each row elementwise
The above code iterates till all the elements in Lower Triangular matrix becomes zero. Later we get the tril and triu of A, which gives L and U matrices.
I suggest using breakpoints, and work on this program. To know the updation of A for each iteration.
Hope this helps!
  2 件のコメント
Nathan Nguyen
Nathan Nguyen 2020 年 1 月 29 日
Is doing the breakpoint thing that you are suggesting easy to implement..? I have never used a breakpoint before.
I read that clicking on the line setsa breakpoint, but that doesn't really accomplish much.
Harsha Priya Daggubati
Harsha Priya Daggubati 2020 年 1 月 29 日
Using a breakpoint and step through each line of the code further to know the value of A at each step and comparing it with manual solving of LU Decomposition Method helps you to know the code better.
Take an example matrix A and try to solve it manually and then execute the program for the same matrix A. I hope this helps you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by