0% found this document useful (0 votes)
15 views2 pages

Comp - Meth Lab - 05

Uploaded by

tesfahun demisew
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)
15 views2 pages

Comp - Meth Lab - 05

Uploaded by

tesfahun demisew
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/ 2

Introduction to Computational Methods Laboratory Manual Semester II, 2015GC

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

2. Gauss Seidel Algorithm Pseudo Code


Procedure gauss_seidel
input: a(n×n matrix)
b (n×1 right hand vector)
x (n×1 matrix for initial guess)
tol (error tolerance tol)
Output: sol (n×1 output vector)
iter (iteration number)
err (error at the last iterations)
nr<-number of rows of matrix a
nc<-number of columns of matrix a
% code to decompose nXn matrix to lower triangular matrix and upper triangular matrix
for i=1:nr
for j=1:nc
if i>j||i==j
dij=aij;
%nXn matrix which is intended to be used for storing all the diagonal elements
%all elements below the diagonal of matrix a
else
dij=0;
end if
end for j
end for i

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

n_iter=0; %number of iterations


while err>tol %iterate till the required error level is achieved
sol=inv(d)*(b-(c*x)); %Formula to solve the nth iteration using Gauss-seidel algorithim
x=sol;
err=max(abs(b-a*x)); %calculates the current error
n_iter=n_iter+1; %number of iterations
end while
end procedure gauss_seidel

__________________________________________________________________________________________________
AAU/AAiT, SECE Semester II, 2015GC

You might also like