Linear Algebra - LU Decomposition using MATLAB

In summary: 0 0 4.0000 1.0000 1.0000 0 0 6.0000 1.0000 1.0000 0 0 8.0000 1.3333 1.0000 0 0 2.0000 0 0.3077 1.0000
  • #1
tironel
11
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
  • #2
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?
 
  • #3
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
 

Related to Linear Algebra - LU Decomposition using MATLAB

1. What is Linear Algebra?

Linear Algebra is a branch of mathematics that deals with linear equations, matrices, and vector spaces. It involves the study of linear transformations and their representations in vector spaces. It is used in various fields such as physics, engineering, computer science, and economics.

2. What is Matlab?

Matlab is a high-level programming language and interactive environment for numerical computation, visualization, and programming. It is commonly used in scientific and engineering applications for data analysis, simulation, and algorithm development. It also has built-in tools for linear algebra operations.

3. How is Linear Algebra used in Matlab?

Matlab has a built-in linear algebra library that allows users to perform various operations such as matrix multiplication, solving linear systems of equations, and finding eigenvalues and eigenvectors. It also has functions for creating and manipulating matrices and vectors.

4. What are the benefits of using Matlab for Linear Algebra?

Matlab provides a user-friendly interface for performing complex linear algebra operations. It also has a vast library of built-in functions and tools specifically designed for linear algebra, making it a powerful tool for solving mathematical problems efficiently. Additionally, Matlab's visualization capabilities can help users better understand and interpret the results of their linear algebra computations.

5. Can Matlab be used for more advanced topics in Linear Algebra?

Yes, Matlab can be used for advanced topics in Linear Algebra such as numerical methods for solving systems of linear equations, singular value decomposition, and least squares regression. It also has tools for working with sparse matrices and performing advanced matrix decompositions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
373
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
850
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Linear and Abstract Algebra
Replies
19
Views
2K
  • Introductory Physics Homework Help
Replies
11
Views
245
Back
Top