Sie sind auf Seite 1von 12

University of Engineering and Technology Peshawar

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:

 Interchange positions of two rows.

 Multiply one of the rows by a nonzero scalar.

 Add or subtract the scalar multiple of one row to another row.

Consider a system of equations

𝑎11 𝑥 + 𝑎12 𝑦 + 𝑎13 𝑧 = 𝑏1

𝑎21 𝑥 + 𝑎22 𝑦 + 𝑎23 𝑧 = 𝑏2

𝑎31 𝑥 + 𝑎32 𝑦 + 𝑎33 𝑧 = 𝑏3

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

By performing row operations the matrix is reduced to Echelon Form.

𝑎11 𝑎12 𝑎13 𝑏1


[ 0 𝑎22 𝑎23 𝑏2 ]
0 0 𝑎33 𝑏3

Gaussian Elimination with Partial Pivoting


In partial pivoting the row with largest first element in a row is interchanged with first row to
reduce errors. In this way row operations are performed with smaller elements.

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

Power Method for Finding Eigen Values


The power method is a numerical method for estimating the largest absolute dominant
eigenvalue and a corresponding eigenvector for a matrix. In general it can be written as

𝐴𝑋𝑖−1 = 𝜆𝑖 𝑥𝑖

Where A is the coefficient matrix, X is the initial approximation 𝜆 is the largest Eigen value and x
is the corresponding Eigen vector.

Jacobi Method for Finding Eigen Values


In numerical analysis, the Jacobi eigenvalue algorithm is an iterative method for the calculation
of the eigenvalues and eigenvectors of a real symmetric matrix. It computes all Eigen values and
Eigen vectors. In this method an orthogonal matrix is constructed such as

𝑎11 𝑎12 𝑎13


[𝑎21 𝑎22 𝑎23 ]
𝑎31 𝑎32 𝑎33

2aiJ
tan 2θ =
aii − ajj

Where i = row number and j = column number

aiJ must be largest absolute number among the off-diagonal elements.


cos θ − sin θ
S=[ ]
sin θ cos θ
An orthogonal matrix is created in such a way that positions used in the angle formula creates a
square of sin ans cos as shown in above equation. Remaining diagonal elements will be 1 and
off-diagonal will be zero.

𝐷 = 𝑆 −1 𝐴𝑆

This process is continued until D is a diagonal matrix.

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 Gauss Jordan


%Gauss Jordan by Umair Ali Shah for system of linear equations
clear all;
clc;
A=[2 3 2 4; % Write co-efficient matrix here
4 3 5 9;
3 2 9 7;
7 2 5 4];
b=[3;4;6;7]; % b is the right hand side of the equations
C=[A b]; % Augmented matrix
U=rref(C); % Reduce row echelon form
[r, c]=size(U); % Determining rows and columns of a matrix
disp('Reduced Echelon Form')
disp(U)
% For Square Matrix
if c-1==r
g=U(:, length(U));
o=length(g);
for i=1:o
x(i)=g(i);
fprintf('x%d',i)
fprintf(' = %5.4f\n', x(i))
end
end
% 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
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
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 Power Method


%Power method for finding Eigen values by Umair Ali Shah
clear all;
clc;
A=[2 3 2 4;
4 3 5 9;
3 2 9 7;
7 2 5 4];
[r, c]=size(A);
if r==c
b=ones(c,1);
lamda=zeros(1,100);
ae=input('Enter the allowed error like 0.01 or 1e-2 for accurate upto two
decimal places: ');
fprintf('Iter. No. Eigen Values Corresponding Eigen Vectors\n')
for i=2:20
b=(A*b)/max(A*b);
lamda(i)=max(A*b); % Accumulating eigen values from the loop
fprintf('%d %5.4f ',i-1, lamda(i))
for n=1:r
fprintf('%5.4f ',b(n)) % Printing each value of vector space
end
fprintf('\n')
if abs(lamda(i)-lamda(i-1))<=ae
tr=(lamda(i)); % Largest Eigen Value
break
end
end
% Exemption of unnecessary data
nonz=(abs(tr-lamda(2:i)).*100)./tr;
plot(1:length(nonz),nonz,1:length(nonz),nonz,'.r');
title('Relative Percent Error vs Iteration')
xlabel('Iteration Number')
ylabel('Relative Percent Error %')
else
disp('Matrix is not symmetric')
end
% end of program
This program will determine the Eigen value and vector but matrix must be square.

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

5.0000 -1.0000 1.0000 7.0000

0 -9.8000 -10.2000 -0.4000

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

Both method gives the same root x1 = 1 x2 =1 x3 = 1.

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

Iter. No. Eigen Values Corresponding Eigen Vectors

1 18.5714 0.5238 1.0000 1.0000 0.8571

2 17.7615 0.5103 0.9590 1.0000 0.7590

3 17.7254 0.5030 0.9430 1.0000 0.7615

4 17.6994 0.5010 0.9418 1.0000 0.7590

5 17.6950 0.5008 0.9413 1.0000 0.7586

6 17.6942 0.5007 0.9412 1.0000 0.7585

7 17.6940 0.5007 0.9412 1.0000 0.7585

8 17.6939 0.5007 0.9412 1.0000 0.7585

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

6.1623 -0.0000 3.3766

-0.0000 -0.1623 1.2643

3.3766 1.2643 4.0000

Enter 1 if the above matrix is diagonal and 0 if not : 0

Maximum Row Index Column Index

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

Enter 1 if the above matrix is diagonal and 0 if not : 1

Eigen Values

8.6266 0.0000 1.5357

Columns are the Eigen Vectors

0.6553 -0.4723 -0.5895

0 0 0

0.4782 -0.3447 0.8078

Engineering Problem (Optimal Planning, Industrial Engineering)


A soap company produces two lotion soaps, Creamy and Creamy Plus, in large batches. A batch
of Creamy contains 1 ton of oil and 16 tons of base, while a batch of Creamy Plus contains 2 ton
of oil and 16 tons of base. In inventory company has 12 tons of oil and 112 tons of base. How
many batches of each type should be produced?

Solution
Let

x1 be the number of batches of Creamy

x2 be the number of batches of Creamy Plus

the amount of oil used and availability in inventory is

1x1+ 2x2 =12

and the amount of base and availability in inventory is

16 x1+ 16x2 =112

 Enter A and b in MATLAB as shown below and run the file.


A=[1 2;
16 16];
b=[12;112];
 As the matrix is square so it will be solved automatically.

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.

Das könnte Ihnen auch gefallen