Below I have a code written for solving the L U decomposition of a system of equations however I need my code to just output the answers with this format it outputs the variables in the matrix for example i need the function to output x [1;2;3;4] any suggestions?
function[L,U,X]=LU_Parker(A,B)
[m n]=size(A);
if (m ~= n )
disp ( 'LR2 error: Matrix must be square' );
return;
end;
% Part 2 : Decomposition of matrix into L and U
L=zeros(m,m);
U=zeros(m,m);
for i=1:m
% Finding L
for k=1:i-1
L(i,k)=A(i,k);
for j=1:k-1
L(i,k)= L(i,k)-L(i,j)*U(j,k);
end
L(i,k) = L(i,k)/U(k,k);
end
% Finding U
for k=i:m
U(i,k) = A(i,k);
for j=1:i-1
U(i,k)= U(i,k)-L(i,j)*U(j,k);
end
end
end
for i=1:m
L(i,i)=1;
end
% Program shows U and L
U
L
% Now use a vector y to solve 'Ly=b'
y=zeros(m,1);
y(1)=B(1)/L(1,1);
for i=2:m
y(i)=-L(i,1)*y(1);
for k=2:i-1
y(i)=y(i)-L(i,k)*y(k);
y(i)=(B(i)+y(i))/L(i,i);
end;
end;
% Now we use this y to solve Ux = y
x=zeros(m,1);
x(1)=y(1)/U(1,1);
for i=2:m
x(i)=-U(i,1)*x(1);
for k=i:m
x(i)=x(i)-U(i,k)*x(k);
x(i)=(y(i)+x(i))/U(i,i);
end;
end

댓글 수: 2

Daz
Daz 2015년 2월 3일
Aren't you going to get a divide by 0 error? At the very end of what I quoted, you have L(i,k) = L(i,k)/U(k,k);
But the first time through, U is a zero matrix.
L=zeros(m,m); U=zeros(m,m); for i=1:m % Finding L for k=1:i-1 L(i,k)=A(i,k); for j=1:k-1 L(i,k)= L(i,k)-L(i,j)*U(j,k); end L(i,k) = L(i,k)/U(k,k); end
ela mti
ela mti 2020년 11월 17일
is "i" a counter that shows how many time should loop be done?could you explain that to me?and also "k" and "j" are counter for rows and coluomn?is that so?

댓글을 달려면 로그인하십시오.

답변 (7개)

Oleg Komarov
Oleg Komarov 2011년 2월 15일

1 개 추천

Matlab is case-sensitive, if you want to store the output of x then in the first line change X to lowercase.
Oleg
Mohamed Said Attia
Mohamed Said Attia 2011년 6월 4일

1 개 추천

*there is a problem with the way you are solving the equation to get y & x try*
% Now use a vector y to solve 'Ly=b'
y=zeros(m,1); % initiation for y
y(1)=B(1)/L(1,1);
for i=2:m
%y(i)=B(i)-L(i,1)*y(1)-L(i,2)*y(2)-L(i,3)*y(3);
y(i)=-L(i,1)*y(1);
for k=2:i-1
y(i)=y(i)-L(i,k)*y(k);
end;
y(i)=(B(i)+y(i))/L(i,i);
end;
y
% Now we use this y to solve Ux = y
x=zeros(m,1);
x(m)=y(m)/U(m,m);
i=m-1;
q=0;
while (i~= 0)
x(i)=-U(i,m)*x(m);
q=i+1;
while (q~=m)
x(i)=x(i)-U(i,q)*x(q);
q=q+1;
end;
x(i)=(y(i)+x(i))/U(i,i);
i=i-1;
end;
x

댓글 수: 3

ela mti
ela mti 2020년 11월 17일
would you explain to me this part and what is q ?
i=m-1;
q=0;
while (i~= 0)
x(i)=-U(i,m)*x(m);
q=i+1;
while (q~=m)
x(i)=x(i)-U(i,q)*x(q);
q=q+1;
end;
x(i)=(y(i)+x(i))/U(i,i);
i=i-1;
end;
x
THEOPHILUS GODFREEY
THEOPHILUS GODFREEY 2021년 5월 12일
undefined function or variable m
Walter Roberson
Walter Roberson 2021년 5월 12일
Refer back to the original question; the Answer here only shows the changes instead of copying everything before then as well.

댓글을 달려면 로그인하십시오.

Mohamed Said Attia
Mohamed Said Attia 2011년 6월 4일

1 개 추천

and when you call the function from matlab use
[L,U,X]=LU_Parker(A,B) not LU_Parker(A,B)

댓글 수: 1

Walter Roberson
Walter Roberson 2011년 6월 4일
Not really relevant: if you do not specify output variables and do not put a semi-colon at the end of the line, you will get
ans =
for each of the output variables, in left-to-right order.

댓글을 달려면 로그인하십시오.

John
John 2011년 2월 15일

0 개 추천

I tried this but it still outputs my answer the same way, I originally had it as a lowercase x but I changed it to upper case after I realized it didn't change anything.

댓글 수: 1

Oleg Komarov
Oleg Komarov 2011년 2월 15일
Then can you post the undesired result and the desired one? It's not very clear from your first description.

댓글을 달려면 로그인하십시오.

Tan Edwin
Tan Edwin 2011년 2월 15일

0 개 추천

Maybe u can try adding X=x to allow it to ouput the values of x?
not sure if this is what u want.
edwin
John
John 2011년 2월 15일

0 개 추천

Yes, redefining the x like you said allowed the function to output what I was needing, however I must have an error in my coding because I inputed the following matrices and got the following answer but I am getting a 0 for one of the answers which should not be there. Any possible solutions?
INPUT
A=[ 6 0 0 0 0; 0 1 0 -2 0; 1 0 -3 0 0; 0 8 -4 -3 -2; 0 2 0 0 -1];
B=[1;0;0;1;0];
LU_Parker(A,B)
Output:
X =
0.1667
0
0.0432
0.1841
1.7778
ans =
1.0000 0 0 0 0
0 1.0000 0 0 0
0.1667 0 1.0000 0 0
0 8.0000 1.3333 1.0000 0
0 2.0000 0 0.3077 1.0000
Mahesh Prajapati
Mahesh Prajapati 2020년 9월 21일
편집: Walter Roberson 2020년 12월 30일

0 개 추천

any suggestions?
function[L,U,X]=LU_Parker(A,B)
[m n]=size(A);
if (m ~= n ) disp ( 'LR2 error: Matrix must be square' ); return; end;
% Part 2 : Decomposition of matrix into L and U
L=zeros(m,m);
U=zeros(m,m);
for i=1:m
% Finding L
for k=1:i-1
L(i,k)=A(i,k);
for j=1:k-1
L(i,k)= L(i,k)-L(i,j)*U(j,k);
end
L(i,k) = L(i,k)/U(k,k);
end
% Finding U
for k=i:m
U(i,k) = A(i,k);
for j=1:i-1
U(i,k)= U(i,k)-L(i,j)*U(j,k);
end
end
end
for i=1:m
L(i,i)=1;
end
% Program shows U and L U L
% Now use a vector y to solve 'Ly=b'
y=zeros(m,1);
y(1)=B(1)/L(1,1);
for i=2:m
y(i)=-L(i,1)*y(1);
for k=2:i-1
y(i)=y(i)-L(i,k)*y(k);
y(i)=(B(i)+y(i))/L(i,i);
end;
end;
% Now we use this y to solve Ux = y
x=zeros(m,1);
x(1)=y(1)/U(1,1);
for i=2:m
x(i)=-U(i,1)*x(1);
for k=i:m
x(i)=x(i)-U(i,k)*x(k);
x(i)=(y(i)+x(i))/U(i,i);
end;
end

댓글 수: 3

Walter Roberson
Walter Roberson 2020년 12월 30일
Suggestions about what?
One thing I would suggest is that you format the code you post.
Gudi Vara Prasad
Gudi Vara Prasad 2021년 4월 20일
편집: Gudi Vara Prasad 2021년 4월 20일
% There is some mistake with the Back Substituion at the end in the above code. Please check it again..
clc;
clear all;
close all;
format 'short';
% input:
% A = coefficient matrix
% B = right hand side vector
% output:
% x = solution vector
disp('Application of LU Decomposition')
tic
A = % user input
B = % user input
[m, n] = size(A);
if m ~= n, error('Matrix A must be square'); end
% Decomposition of matrix into L and U :
L=zeros(m,m);
U=zeros(m,m);
for i=1:m
% Finding L
for k=1:i-1
L(i,k)=A(i,k);
for j=1:k-1
L(i,k)= L(i,k)-L(i,j)*U(j,k);
end
L(i,k) = L(i,k)/U(k,k);
end
% Finding U
for k=i:m
U(i,k) = A(i,k);
for j=1:i-1
U(i,k)= U(i,k)-L(i,j)*U(j,k);
end
end
end
for i=1:m
L(i,i)=1;
end
% Program shows U and L U L :
% Now use a vector y to solve 'Ly=b' :
y=zeros(m,1);
y(1)=B(1)/L(1,1);
for i=2:m
y(i)=-L(i,1)*y(1);
for k=2:i-1
y(i)=y(i)-L(i,k)*y(k);
y(i)=(B(i)+y(i))/L(i,i);
end
end
fprintf("Lower Decomposition Traingle = ")
L
fprintf("Upper Decomposition Traingle = ")
U
% Now we use this y to solve Ux = y
% x=zeros(m,1);
% x(1)=y(1)/U(1,1);
% for i=2:m
% x(i)=-U(i,1)*x(1);
% for k=i:m
% x(i)=x(i)-U(i,k)*x(k);
% x(i)=(y(i)+x(i))/U(i,i);
% end
% end
% Back substitution :
x = zeros(n, 1);
AM = [U B];
x(n) = AM(n, n+1) / AM(n, n);
for i = n - 1: - 1:1
x(i) = (AM(i, n+1) - AM(i, i + 1:n) * x(i + 1:n)) / AM(i, i);
end
fprintf("Solution of the system is = ")
x
Edmar
Edmar 2024년 2월 25일
Do you have sample of code in LU Decomposition for a 24x24 matrix using MatLab

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Statistics and Linear Algebra에 대해 자세히 알아보기

질문:

2011년 2월 15일

댓글:

2024년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by