Beruflich Dokumente
Kultur Dokumente
Jalozai Campus
Department of Industrial Engineering
Numerical Analysis & Computer Applications Lab
Lab No. : 04
Submitted to: Dr. Iltaf
Submitted by: Umair Ali Shah
Semester: 5th Semester
Registration ID: 18JZIND0105
Submission Date: Dec 20, 2020
Lab 03
To Find the Roots of System of Linear Equations and Finding Eigen
Values and Eigen vectors
Objectives
To write MATLAB codes for solving system of linear equations by Gauss Elimination,
Jordan and Partial Pivoting.
To write MATLAB programs for finding Eigen values and Eigen vectors using Power
method and Jacobi method.
Implementation of these methods on engineering case studies using solution of nonlinear
models.
Introduction
In this lab we will develop MATLAB codes to find the roots of system of linear equations using
Gauss Elimination, Jordan and Partial Pivoting. We will also find Eigen values and Eigen vectors
using Power and Jacobi method. We will then solve some engineering problems using these
methods.
Theory
Gauss Elimination
Gaussian elimination, also known as row reduction, is an algorithm in used for solving a system
of linear equations. In Gauss elimination matrix is reduced to echelon form. It relies upon
three elementary row operations that can be used on a matrix:
Right side of the system of equations will be written with coefficient matrix to form augmented
matrix.
𝑎11 𝑎12 𝑎13 𝑏1
[𝑎21 𝑎22 𝑎23 𝑏2 ]
𝑎31 𝑎32 𝑎33 𝑏3
Gauss Jordan
The only difference between Gauss elimination and Gauss Jordan is that Gauss Jordan puts
matrix in Reduced Row Echelon Form.
1 0 0 𝑏1
[0 1 0 𝑏2 ]
0 0 1 𝑏3
𝐴𝑋𝑖−1 = 𝜆𝑖 𝑥𝑖
Where A is the coefficient matrix, X is the initial approximation 𝜆 is the largest Eigen value and x
is the corresponding Eigen vector.
2aiJ
tan 2θ =
aii − ajj
𝐷 = 𝑆 −1 𝐴𝑆
MATLAB Programs
MATLAB Program for Gauss Elimination with Partial Pivoting
%Gauss elimination by Umair Ali Shah for system of linear equations
clear all;
clc;
A=[2 3 2 4 9; % Write matrix here
4 3 5 9 7;
3 2 9 7 4;
7 2 5 4 3];
b=[3;4;6;8]; % b is the right hand side fo the equations
C=[A b]; % Augmented matrix
[L, U]=lu(C); %U is the Reduced echelon form of C with partial pivoting
[r, c]=size(U);
disp('Echelon Form')
disp(U)
% For Rectangular Matrix
if (c-1)>r
for s=r+1:1:(c-1)
x(s)=input('Enter the value of free variables : ');
fprintf('x%d',s)
fprintf(' = %5.4f\n',x(s))
end
end
for i=r:-1:1
a=U(i,c);
for n=1:c-1
if n>i
a=a-(x(n)*U(i,n));
end
end
x(i)=a/U(i,i);
fprintf('x%d',i)
fprintf(' = %5.4f\n',x(i))
end
This program will solve the system of equation automatically if the matrix is square. In case
matrix is rectangular it will ask for the values of free variables.
MATLAB Program for Jacobi Method for Eigen Values and Vectors
%Jacobi method for finding Eigen values by Umair Ali Shah
clear all;
clc;
A=[4 3 2;
3 2 3;
2 3 4];
[r,c]=size(A);
if A==A' % Checking the symmetricity of matrix
for w=2:10
S=zeros(r,c); % Pre-allocation of orthogonal matrix
B=A;
mxn=zeros(3,3);% row 1= max no, row 2 = row index of no. row 3= column
index of no
for i=1:r % Making diagonal elements zero to pick max non-diagonal
element
B(i,i)=0;
end
B=abs(B);
for g=1:c % g is the row and ci is the column
[mx, ci]=max(B(g,:));
mxn(1,g)=mx;
mxn(2,g)=g;
mxn(3,g)=ci;
end
[M, FI]=max(mxn(1,:));
m=mxn(2,FI); % row index of final max
n=mxn(3,FI); % column index of final max
fprintf('Maximum Row Index Column Index\n')
fprintf('%d %d %d\n',M,m,n)
e=2*A(m,n)/(A(m,m)-A(n,n));% tan2theta
if e==-Inf % matlab also considers -ive sign with infinity
e=Inf;
end
T=atand(e)/2;% theta
S(m,m)=cosd(T); % creating orthogonal matrix
S(m,n)=-sind(T);
S(n,m)=sind(T);
S(n,n)=cosd(T);
if m==1 && n==2
S(n+m,n+m)=1;
end
if m==1 && n==3
S(n-m,n-m)=1;
end
if (S)*(S)'~=eye(r,c)% Checking that the matrix is orthogonal or not
disp(S)
S=input('Complete the above matrix to form orthogonal matrix and write in
the matrix form : ')
end
disp('S')
disp(S)
L{w}=dataset;% Accumulating data for finding eigen vectors
L{1}=eye(r,c);
L{w}=S;
EV=L{w}*L{w-1};
D=pinv(S)*A*S;
disp('D')
disp(D)
h=input('Enter 1 if the above matrix is diagonal and 0 if not : ');
if h==1 % In 2015 or higher version isdiag(D) can e used
eigen=diag(D);
disp('Eigen Values')
for z=1:r
x(z)=eigen(z);
fprintf('%5.4f ',x(z))
end
fprintf('\n')
disp('Columns are the Eigen Vectors')
disp(EV)
break
else
A=D;
end
end
else
disp('Matrix is not symmetric')
end
This program determines Eigen values and Eigen vectors if the matrix is symmetric. It will ask
for to check that the matrix is diagonal or non-diagonal after each iteration.
Problems
Problem 1
Using Gauss Elimination and Gauss Jordan find the solution of the linear system.
5x1 – x2 + x3 = 7
x1 – 10x2 – 10x3 = 1
– x1 + 4x2 + 5x3 = 0
Solution
Solution
Enter the coefficient matrix with variable A.
Write right hand side of the equation as a column matrix with variable b and run the file.
Gauss Elimination
Echelon Form
0 0 1.2449 1.2449
x3 = 1.0000
x2 = -1.0000
x1 = 1.0000
Gauss Jordan
Reduced Echelon Form
1 0 0 1
0 1 0 -1
0 0 1 1
x1 = 1.0000
x2 = -1.0000
x3 = 1.0000
Problem 2
Find Eigen value and corresponding vector for the following matrix by power method.
[2 3 2 4;
4 3 5 9;
3 2 9 7;
7 2 5 4];
Solution
Enter the above matrix with variable A in MATLAB and run the file.
Results
Enter the allowed error like 0.01 or 1e-2 for accurate up to two decimal places: 1e-4
Figure 4.1: Relative Percent Error vs Iteration Number Graph for Problem 2
Problem 3
Find Eigen values and vector by Jacobi method
[4 3 2;
3 2 3;
2 3 4];
Solution
Enter the matrix with variable A and run the file.
After each iteration it will ask to check that matrix is diagonal or not.
Enter 1 if the matrix is diagonal and 0 if not.
Results
Maximum Row Index Column Index
3 1 2
0.8112 -0.5847 0
0.5847 0.8112 0
0 0 1.0000
3.376615e+00 3 1
0.8078 0 -0.5895
0 0 0
0.5895 0 0.8078
8.6266 0 -0.0000
0 0 0
-0.0000 0 1.5357
Eigen Values
0 0 0
Solution
Let
Results
Reduced Echelon Form
1 0 2
0 1 5
x1 = 2.0000
x2 = 5.0000
The company should produce x1 = 2 batches Creamy and x1 = 5 batches of Creamy Plus to finish
inventory
Conclusion
From this lab we concluded that partial pivoting makes Gaussian elimination easy by moving
large number to the leading entry. In Gauss Jordan we need no backward substitution as in
Gauss elimination but putting to reduced row echelon takes time. Power method is simple but
computes only the largest Eigen value while Jacobi is complex but computes all Eigen values
and vectors.