Linear Algebra - LU Decomposition using MATLAB

Click For Summary
SUMMARY

The discussion focuses on implementing LU decomposition in MATLAB using the function LU_Parker. The user seeks to modify the function to output the solution vector x in a specific format, while also addressing issues related to variable naming and unexpected zero values in the output. Key suggestions include ensuring consistent variable naming due to MATLAB's case sensitivity and debugging the code to resolve the zero output for one of the solution components.

PREREQUISITES
  • Understanding of LU decomposition in linear algebra
  • Familiarity with MATLAB programming and syntax
  • Knowledge of matrix operations and manipulation
  • Experience with debugging MATLAB code
NEXT STEPS
  • Review MATLAB variable naming conventions and case sensitivity
  • Learn about MATLAB's matrix indexing and manipulation techniques
  • Explore debugging techniques in MATLAB to identify logical errors
  • Study LU decomposition algorithms and their implementation in MATLAB
USEFUL FOR

Students, researchers, and professionals in mathematics, engineering, and computer science who are working with linear algebra and MATLAB programming, particularly those focused on solving systems of equations using LU decomposition.

tironel
Messages
10
Reaction score
0
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?

Code:
   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
 
Physics news on Phys.org
Could you rephrase the question? It's a little hard to understand you without any punctuation.

One comment about your code: you define the variable x, but the function returns X. Variable names in Matlab are case sensitive. Maybe that's the problem?
 
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

Code:
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:
Code:
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
 

Similar threads

Replies
0
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
1
Views
3K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K