Assignment 2

advertisement
ATILIM UNIVERSITY
Department of Computer Engineering
COMPE 350 Numerical Methods
2008-09 (II)
Assignment 2
Instructors: Ali Yazıcı, C. Fügen Selbes
Date posted on web: 7 May, 2009
Due date: 20 May, 2009 by 17:00 hrs
PART A: (Statement of the problem)
The goal of this assignment is to write MATLAB functions for the tridiagonal system Ax=b,
using iterative methods and then run some numerical experiments. For a tridiagonal matrix
A, we store only the entries from the main diagonal (in a vector d of length n), and the
entries from the sub-diagonal and super-diagonal in two vectors a and c respectively, of
length n - 1. In this assignment, the tridiagonal matrix has the following form (for n=4)
d1
a
A=  1
0

0
c1
d2
0
c2
a2
0
d3
a3
0
0 
c3 

d4 
PART B: (Programming)
a) Iterative Method (Gauss-Jacobi) (Script File):
Write a MATLAB function named jacobi3d.m for the Gauss-Jacobi Method to solve
the system of linear equations Ax=b where A is a tridiagonal matrix. This MATLAB
function should accept the input parameters as shown below then, it should return the
solution vector and the number of iterations. This MATLAB function have the form:
function [x,ite]=jacobi3d(a, d, c, b, x0, eps)
where
a: The sub-diagonal of the coefficient matrix of the system Ax=b in a vector.
d: The main diagonal of the coefficient matrix of the system Ax=b in a vector.
c: The super-diagonal of the coefficient matrix of the system Ax=b in a vector.
b: The right hand side column vector.
x0: is the initial vector x 0 .
eps: accuracy for ending the iterations. Iterations are performed until the following
relative error criteria is satisfied:
x new  xold
x new
where

 eps ,


is the infinity vector norm defined as the maximum over all the
absolute values elements of the vector :
x

 max( x1 , x2 ,..., xn ) . In
2
MATLAB, you can use the norm(x, inf) to compute the infinity norm of a
vector.
x: is the solution vector.
ite: is the number of iterations performed.
b) Iterative Method (Gauss-Seidel) (Script File):
Write a MATLAB function named gseidel3d.m for the Gauss-Seidel Method to solve
the system of linear equations Ax=b where A is a tridiagonal matrix. This MATLAB
function should accept the input parameters as shown below then, it should return the
solution vector and the number of iterations. This MATLAB function have the form:
function [x,ite]=gseidel3d(a, d, c, b, x0, eps)
where
a: The sub-diagonal of the coefficient matrix of the system Ax=b in a vector.
d: The main diagonal of the coefficient matrix of the system Ax=b in a vector.
c: The super-diagonal of the coefficient matrix of the system Ax=b in a vector.
b: The right hand side column vector.
x0: is the initial vector.
eps: accuracy for ending the iterations. Iterations are performed until the following
relative error criteria is satisfied:
x new  xold
x new
where

 eps ,


is the infinity vector norm defined as the maximum over all the
absolute values elements of the vector :
x

 max( x1 , x2 ,..., xn ) . In
MATLAB, you can use the norm(x, inf) to compute the infinity norm of a
vector.
x: is the solution vector.
ite: is the number of iterations performed.
PART C: (Implementations)
a) Solve the tridagonal system given below by using the methods of Jacobi (jacobi3d.m) and
Gauss-Seidel (gseidel3d.m). Take the tolerance as eps=1e-6 and the initial vector as
x0=rand(1,4)*1e-4
 2  1 0 0   x1  1
 1 2  1 0   x  0

  2    
 0  1 2  1  x3  0


 0 0  1 2   x 4  1
3
b) The tridiagonal system given in part (a) has the size n=4 where the main diagonals are 2;
the super and the sub-diagonal values are -1; the right hand side is a zero vector except
the first and the last values are 1. This can be generalized to a tridiagonal system of size
n.
Solve this tridagonal system for various n values of 8, 16, 32, and 64 by using the methods
of Jacobi (jacobi3d.m) and Gauss-Seidel (gseidel3d.m).
In solving this part, write a MATLAB function named maintridiagonal.m that
generates this tridiagonal system and then calls the jacobi3d.m and gseidel3d.m to solve
it. Hence, the general form will be
function maintridiagonal(n)
where n is the size of the tridiagonal system
The tridiagonal system can be generated by the following MATLAB commands:
d
a
c
b
=
=
=
=
2 * ones(1, n); % a vector of n values for the diagonal
-ones(1, n - 1); %a vector of n-1 values below the diagonal
-ones(1, n - 1); % a vector of n-1 values over the diagonal
0 * ones(1, n); b(1) = 1; b(n) = 1;
Take the tolerance as eps=1e-6 and the initial vector x 0 as follows:
x0 = rand(1, n) * 1e - 4
Notice that x0 and d are row vectors. You can use transpose (‘) to make them column
vectors.
c) Compare the convergence of the Jacobi method and the Gauss-Seidel method for the
model problem and the following set of values for n
nvalues = [4; 8; 16; 32; 64]
Make a table with three columns: n, number of iterations needed by Jacobi, and number
of iterations needed by Gauss-Seidel.
NOTES:
 This is a group assignment with 3 students in each.
 Mail your homework to compe351@atilim.edu.tr.
 Attach the report(word document) and the script file(s) to your mail.
 Cheating is strictly forbidden.
 Late homework will be graded out of 10-d2/2 (d is the number of late days).
 Don’t forget to write your name.
 Don’t forget to insert page numbers.
4
 Report should include the following
o A cover page (University, Faculty, Dept. Names, Atılım Logo, CourseCode,
Course Title, Group Members with Student ID numbers, Section)
o Table of Contents page
o Content of the report organized into sections as follows:
1. Introduction
o Statement of the problem
o Pseudo-code of each algorithm used
2. Implementation
o Computer system used
o Program listings
3. Tests and results
o Results should be summarized in a table
o A comparative discussion of the results.
o A discussion on unexpected results and reasons for each.
4. List of References
Download