Beruflich Dokumente
Kultur Dokumente
Topics
Mathematics: Gaussian elimination; limitations of this basic method. MATLAB: writing code for row operations, back substitution and basic partial pivoting.
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).
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.