Académique Documents
Professionnel Documents
Culture Documents
SCHOOL OF ELECTRICAL, ELECTRONIC & COMMUNICATION ENG. EEEN30150 MODELLING AND SIMULATION
Experiment MS2
>> [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.
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.