Vous êtes sur la page 1sur 4

UNIVERSITY COLLEGE DUBLIN

SCHOOL OF ELECTRICAL, ELECTRONIC & COMMUNICATION ENG. EEEN30150 MODELLING AND SIMULATION
Experiment MS2

SOLUTION OF SYSTEMS OF LINEAR EQUATIONS


1. Objective: To implement Gaussian elimination (with partial or total pivoting) and Gauss-Seidel iteration in Matlab as function M-files. 2. Background Information: See module notes and handouts for experiments MS0_1 and MS0_2. As in previous experiment you may solve problems using just the Matlab command line, using loops, using inline functions or using function M-files. However, in this experiment note that the questions call for the creation of a function M-file to solve the problem. As before your function M-files will have to include help files, version information and error detection. You will also have to avoid bad practice such as choosing meaningless names for variables and/or functions and embedding universal numbers into the code. The module notes discuss the algorithms, sic Gaussian elimination (with partial or total pivoting) and Gauss-Seidel iteration, which you are called upon to employ, but they do not indicate how to implement these algorithms as Matlab function M-files. Some comments on additional Matlab commands which you are likely to find useful are therefore in order. These merely reinforce the module notes where all of these comments have already been made. The Matlab command max for an input argument which is a matrix A, produces a row vector containing the maximum element from each column. If you want to know, not only what the maximum value in a each column is but also where it is, then you employ the max command with output arguments, specifically [Y,I] = max(A) produces a row vector Y which has as its first element the maximum value appearing in the first column of A and so on. But it also produces a row vector I which is the index of that maximum element. So for example: >> A = [0 0.8 -1;0.5 2 0.1;0.75 -3 -0.5] A= 0 0.8000 -1.0000 0.5000 2.0000 0.1000 0.7500 -3.0000 -0.5000
0 .8 1 0 meaning A = 0.5 2 0 .1 . 0 . 75 3 0 . 5

>> [Y,I]=max(A) Y=

0.7500 2.0000

0.1000

I= 3 2 2

meaning that the maximum value in the first column of A is 0.75 and it occurs as the third element in that column; the maximum value in the second column of A is 2 and it occurs as the second element in that column; the maximum value in the third column of A is 0.1 and it also occurs as the second element in that column. So the index of the maximum element in the first column of matrix A is I(1). There is a very neat idea in Matlab for implementing the swap of two rows of a matrix. Suppose for 0 .8 1 0 example that the matrix A is as above, i.e. A = 0.5 2 0.1 which as usual is achieved in 0.75 3 0.5 Matlab by the list method: >> A = [0 0.8 -1;0.5 2 0.1;0.75 -3 -0.5] A= 0 0.8000 -1.0000 0.5000 2.0000 0.1000 0.7500 -3.0000 -0.5000
3 Now suppose we wish to swap rows 1 and 3. Create the vector p = 2 which is the simple vector 1 1 2 with row 1 and row 3 being swapped. 3

>> Aswap = A(p,:) Aswap = 0.7500 -3.0000 -0.5000 0.5000 2.0000 0.1000 0 0.8000 -1.0000
0.7 3 0.5 meaning Aswap = 0.5 2 0.1 , so that the required swap of rows 1 and 3 has been achieved 0 0 .8 1 with very little required code. You should be able to work out for yourself how to similarly achieve the swap of two columns of a matrix. An arguably better method for achieving the same result is discussed in the module notes.

The module notes express the steps involved in Gaussian elimination with pivoting in matrix terms. In these terms pivoting is achieved by premultiplication (or left multiplication) by a permutation matrix. The simplest and absolutely the best way of coding the algorithm in Matlab is to explicitly code it in these matrix terms since Matlab is very good with matrices. Accordingly this is how you are required to code the algorithm. Because the challenge of developing the code and the particular challenge in problem 3 of developing a (slightly) new algorithm are, in my opinion, sufficiently high and because, moreover, we will see engineering applications of solving systems of linear equations in the first minor project, there is no accompanying list of Applications problems in this experiment.

3. Experiment: 3.1. Code-development problems:

In each of the problems the system of linear equations to be considered is:


2 x2 + x4 = 0 2 x1 + 2 x 2 + 3 x3 + 2 x 4 = 2 4 x1 + 3 x 2 + x 4 = 7 6 x1 + x 2 6 x3 5 x 4 = 6
(1)

Problem 1: Create a function M-file in Matlab to solve the system of linear equations (1) by Gaussian elimination with partial pivoting. Is it possible to solve using Gaussian elimination without pivoting? Check your results by re-solving the equations using built-in Matlab commands. The section on matrices in experiment MS0_1 offered two ways for you to solve a system of linear equations using built-in Matlab commands. Be aware that in this case, and for obvious reasons, the bulk of the marks will be assigned to your Matlab code implementing the required Gaussian elimination with partial pivoting algorithm. Solution by means of built-in Matlab commands will accrue only 20% of the marks. Problem 2: Create a function M-file in Matlab to solve the system of linear equations (1) by Gaussian elimination with total pivoting. Be aware that in this case, and for obvious reasons, all of the marks will be assigned to your Matlab code implementing the required Gaussian elimination with total pivoting algorithm. Since the system of equations is the same as in the previous problem you should already have solved by means of built-in Matlab commands and thereby gained some credit for that solution. Accordingly you will gain no additional credit for repeating that solution here. Problem 3: Create a function M-file in Matlab to (try to) solve the system of linear equations (1) by Gauss-Seidel iteration. Note that yet again this is the same system of equations as we considered above. This problem is rather a challenge. You will find that the Gauss-Seidel iteration struggles to converge. Explain what is going wrong with reference to the module notes and by calculating the eigenvalues of the appropriate matrix L1U . The section on matrices in MS0_1 presents all of the Matlab commands which you will require to achieve this analysis. To get around the convergence problems adopt the following strategy of mixing elimination with Gauss-Seidel iteration: As a preliminary step, before setting up the iteration, eliminate one variable

for every equation in the system which involves just one or two variables. Repeat this preliminary elimination as many times as possible. In the case of the system of equations above this preliminary work reduces the system from a system of 4 linear equations in 4 unknowns to a system of 2 linear equations in 2 unknowns. This latter system of linear equations can be relatively easily solved by Gauss-Seidel iteration. Show that this is so, both by solving the system and by calculating the eigenvalues of the appropriate matrix L1U . Although the Gauss-Seidel iteration must be implemented as a function M-file, I do not insist that the preliminary elimination process be part of this M-file. You may, if you wish, achieve this elimination from the command line. That said, if you are aiming for the very highest grades (A-, A or A+) then you need to impress and in this situation creating a single function M-file to achieve the complete new algorithm is certainly more impressive.

Vous aimerez peut-être aussi