0% found this document useful (0 votes)
8 views

Computational(LAB-7)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Computational(LAB-7)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment No.

: 07

Experiment Title: Solving System of Linear Equations & Matrix Inversion by


LU Decomposition Method Using MATLAB.

Objectives:
1. To understand and apply LU decomposition for solving systems of linear equations
using MATLAB.
2. To find the inverse of a matrix using LU decomposition method in MATLAB.
3. To compare manual and MATLAB-built LU results for verifying accuracy.

Theory:
LU decomposition is a method in linear algebra used to solve systems of linear equations and
to find the inverse of a matrix. In this method, a square matrix A is broken into two simpler
matrices: a Lower triangular matrix (L) and an Upper triangular matrix (U), so that A = L × U.
This makes complex matrix operations easier and faster.

A lower triangular matrix has all zeros above its main diagonal, while an upper triangular matrix
has all zeros below the main diagonal. Once a matrix is written as LU, it becomes easier to solve
problems step by step using forward and backward substitution.

To solve Ax = b using LU decomposition:

i. First, decompose A into L and U.


ii. Solve Ly = b using forward substitution (start from top to bottom).
iii. Then solve Ux = y using backward substitution (start from bottom to top).

This method is useful when you need to solve many equations with the same matrix A but
different b values. You decompose A once, and reuse L and U each time.

LU decomposition can also be used to find the inverse of a matrix (A⁻¹). Instead of doing direct
inversion, we solve AX = I, where I is the identity matrix. Each column of the identity matrix
gives one equation, and solving all gives us the inverse matrix.

In MATLAB, LU decomposition is simple. The command [L, U] = lu(A) gives L and U.

To find the inverse, we can use ;


A \ eye(size(A)).

LU decomposition is very helpful in engineering and science because it saves time and avoids
complex calculations. It works only if A is a square and non-singular matrix (its determinant is
not zero). This method is reliable and widely used in real-world applications.
MATLAB Code:
MATLAB Code for LU Decomposition Method & Matrix Inversion:

A = [2 1 1; 3 2 3; 1 4 9];
B = [10; 18; 16];
n = 3;

% LU Decomposition
L = eye(n);
U = A;
for i = 1:n-1
for j = i+1:n
L(j,i) = U(j,i)/U(i,i);
U(j,:) = U(j,:) - L(j,i)*U(i,:);
end
end

% Forward Substitution (Ly = B)


y = zeros(n,1);
for i = 1:n
y(i) = B(i) - L(i,1:i-1)*y(1:i-1);
end

% Backward Substitution (Ux = y)


x = zeros(n,1);
for i = n:-1:1
x(i) = (y(i) - U(i,i+1:n)*x(i+1:n)) / U(i,i);
end

% Matrix Inversion using LU


invA = zeros(n);
for k = 1:n
e = zeros(n,1);
e(k) = 1;
y = zeros(n,1);
for i = 1:n
y(i) = e(i) - L(i,1:i-1)*y(1:i-1);
end
x_col = zeros(n,1);
for i = n:-1:1
x_col(i) = (y(i) - U(i,i+1:n)*x_col(i+1:n)) / U(i,i);
end
invA(:,k) = x_col;
end

% Display Results
disp('L ='); disp(L);
disp('U ='); disp(U);
disp('Solution x ='); disp(x);
disp('Inverse of A ='); disp(invA);
Result:

MATLAB Output of LU Decomposition Method & Matrix Inversion:

Discussion:
In this experiment, we solved a system of three linear equations and found the inverse of a matrix using
LU decomposition, both manually and with MATLAB. The results from our manual steps matched
MATLAB’s output, showing that our understanding was correct. LU decomposition helped break the
matrix into two simpler parts, making calculations easier. We also learned that forward and backward
substitution is faster and more stable than directly solving the full system. MATLAB made the process
quicker and reduced chances of mistakes, especially for large systems. This method is very useful when
solving many equations with the same matrix. However, LU decomposition works best when the matrix
does not need pivoting. Overall, this experiment showed that LU decomposition is a simple and reliable
method that is widely used in engineering and science to solve matrix problems efficiently.

You might also like