For Ax=b, How do I make LU decomposition (A=L*U) without using lu function. For example, A is nxn matrix and bis nx1 matrix.

20 views (last 30 days)
clc
clear
A=rand(5)
b=rand(5,1);
A(2:3,1)
n=length(A)
U=A;
for i=1:n
k=i+1
i
U(k:n,i)=U(k:n,i)-U(i,i)*(U(k:n,i)/U(i,i))
end
for k= 1:n
L(k+1:n,k)=A(k+1:n,k)/A(k,k)
end
This is what I have try so far, I am very new to MATLAB like 2 week and genuinely struggle here.

Accepted Answer

David Hill
David Hill on 5 Sep 2020
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);

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!