Sie sind auf Seite 1von 2

Lab 2, Solving linear systems

Topics
Mathematics: Gaussian elimination; limitations of this basic method. MATLAB: writing code for row operations, back substitution and basic partial pivoting.

Basic Gaussian elimination


Gaussian elimination is the basic method for linear system Ax = b where 2 A= 2 1 solving linear systems by row operations. For example, consider the 4 2 1 1 8 , 9 2 b = 1 . 0

The rst row operation in the Gaussian elimination process for this system is: [row 2] = [row 2] [row 1]. Enter the matrix A and column vector b as dened above into MATLAB. Write an M-le that does the row operation [row 2] = [row 2] [row 1]. Remember to do the row operation on the right-hand side vector b as well as on A. Modify your code to a function M-le (called myrowop.m) that performs a specied row operation on a given matrix and column vector: function [newA, newb] = myrowop(A, b, I, J, r) The function myrowop should take as arguments: A: an n n matrix. b: an n 1 column vector. I, J: integers between 1 and n. r: a real number. The function should perform the row operation [row I ] = [row I ] + r[row J ] on the matrix A and vector b. The new matrix newA and vector newb are then returned by the function. Hint: You can either use a for loop to go through each element of row I in turn, or you can operate on the whole row with a single command (see the Mathematical Commands section of the MATLAB guide in the course reader). Use your myrowop function to perform two more row operations to reduce A to an upper-triangular matrix (i.e. all entries below the main diagonal are zero). Hint: Remember that myrowop returns the new matrix and vector as outputs, so to get it to update A and b you can use commands of the form: >> [A, b] = myrowop(A, b, ...) Now write an M-le (mygauss3.m) that will call myrowop to perform the (three) required row operations to reduce a general 3 3 matrix to upper-triangular form. Remember to use semi-colons to suppress unnecessary output. 7

Back substitution
The next step in solving the linear system is to nd x by back substitution. Start by dening x as a 3 1 vector of zeros, and then calculating x(3): x=zeros(3,1); x(3)=b(3)/A(3,3) Now calculate x(2) and then x(1). Hint: The command for x(2) is NOT: x(2)=b(2)/A(2,2). If you cant remember how to do back substituiton, look at your MATH108 lecture notes from last year! Incorporate these command into your M-le mygauss3.m to nd x by back substituion. The only thing your M-le should display is the calculated solution vector x. Checkpoint: Use your M-le to solve this system Ax = b where A is a random 3 3 matrix and b is a vector of 1s. You can dene A and b with the commands >> A = rand(3); >> b = ones(3, 1); Check that the answer given by your M-le is correct (think about how you can do this).

Problems with the basic method


Now try using your M-le to solve Ax = b 0 1 (i) A = 1 1 4 1 where b is a vector of 1s and 2 2 8 , (ii) A= 2 1 3

2 1 2 8 . 1 9

What do you think went wrong? Modify your M-le so that it can cope with this type of problem. Checkpoint: Use your modied M-le to solve these two system Ax = b. Check that the answer given by your M-le is correct.

Practice test question


Modify your mygauss3.m M-le so that it can solve a general system of n linear equations, where n can be any positive integer. (You dont need to worry about pivoting here.) Hint: you will need to use two nested for...end loops to do the elimination process, followed by another two nested for...end loops for the back substitution process. Think about which matrix entries you would need to eliminate (i.e. make into zeros), and the order in which you would eliminate them, if you were doing Gaussian elimination on a large matrix. Your rst set of nested for...end loops should go through these entries in the same order, performing the necessary row operation at each step to eliminate that entry. Your second set of nested loops should perform a similar procedure for back substitution. Checkpoint: Use your code to solve Ax = b where A is a random 10 10 matrix and b is a vector of 1s. Check that the answer given by your M-le is correct

Das könnte Ihnen auch gefallen