Comp - Meth Lab - 05
Comp - Meth Lab - 05
Laboratory Session: 05
Contents:
Jacobi Method Gauss –Seidel method
Objective:
Solving system of linear equation using iterative methods through practical programming using MATLAB and c++
Matlab Tips
If a is nXn matrix:
diag(a) returns the diagonal elements of the matrix
[r,c]=size(a) store the number of rows and columns of matrix a in r and c respectively
inv(a) returns the inverse of matrix a if it exists
Preparations:
All students who are expected to attend the lab are required to understand the basic iterative algorithms of solving linear
system of equation using iterative method which are dealt in the lecture session.
1. Write a Matlab program that solves n×n system of linear equation using Jacobi algorithm. Test your program using the
following parameters:
5 2 1 10 100
a. A= [1 6 2] b=[15] initial guess, guess=[100] Error tolerance =0.00000005
4 3 8 20 100
1 2 3 3 −9
b. A= [4 5 6] b=[2] initial guess, guess=[ 15 ] Error tolerance =0.00000005
7 8 9 1 −6
2. Write a Matlab program that solves n×n system of linear equation using Gauss-seidel algorithm. Test your algorithm using the
following parameters.
5 2 1 10 100
a. A= [1 6 2] b=[15] initial guess, guess=[100] Error tolerance =0.00000005
4 3 8 20 100
1 2 3 3 −9
b. A= [4 5 6] b=[2] initial guess, guess=[ 15 ] Error tolerance =0.00000005
7 8 9 1 −6
3. Compare the two algorithms in terms of their convergence, for the case of 1a and 2a, by changing all elements of the initial
guess to 1000, 10000, 1000000.
Assignment 3:
1. Write a c++ program that solves systems of linear equations using Jacobi method. The program which is going to be developed
should have the following features:
The program should include a mechanism of checking whether the system of linear equation satisfies the sufficient
condition for convergence or not.
The size of the input matrix shall be determined by the user at run time.
Note:
1. The assignment is expected to be submitted individually.
2. The assignment is expected to be submitted in hard copy and softcopy (for demonstration purpose in the lab).
3. The submission date of the assignment is on May 25, 2015 for Monday session attendants and on May 28, 2015GC for
Thursday session attendants.
__________________________________________________________________________________________________
AAU/AAiT, SECE Semester II, 2015GC
Introduction to Computational Methods Laboratory Manual Semester II, 2015GC
1. Jacobi Algorithm Pseudo code
Procedure jacobbi
input: a (n×n matrix)
b (n×1 right hand vector)
x (n×1 matrix for initial guess)
tol (error tolerance)
Output: sol (n×1 output vector)
n_iter (number of iteration)
err(the difference between the right hand vector and the value obtained at the last iteration)
sol = x;
errr = abs(b-a*x);
ad = diag(a); % diag command is used to extract the diagonal elements of a
n_iter=0;
while max(abs(err))>tol
sol = x + 1./ad .* (b-A*x) %Formula used calculate the nth iteration using Jacobi algorithm
x = sol;
err = abs(b - a*x);
n_iter = n_iter+1;
end while
end procedure jacobbi
c=a-d;
% nXn matrix which is intended to be used for storing all elements of matrix a which are found above
% the diagonal of matrix a
%--------------------------------------------------------------------------------------
err=max(abs(b-a*x));
%used to calculate the difference b/n the right side vector and the solution obtained at the current
%iterations as error
__________________________________________________________________________________________________
AAU/AAiT, SECE Semester II, 2015GC