MA2012 Linear Algebra Polytechnic University Fall 2002 Class 2 class2 is a text file. Save this e-mail into your MATLAB working directory (Save as ... File... choose a name, like class2.txt). Open MATLAB, set your current directory to your working directory. Among the files you should see class2.txt. By clicking twice on this file you can open it. MATLAB will open the file using its own text editor. If you want to try something out, just cut and paste the command into the Command Window of the MATLAB. Hit the enter key to evaluate the command. Since it is a text file, you can use any other text editor/ viewer to open the file, if you wish. ************************************************************************************************** MA2012 Linear Algebra Polytechnic University Fall 2002 Solving systems of linear equations The Gauss-Jordan Elimination Example 1: Solve the following system of linear equations over the real numbers: x+y+4z=15 2x+4y-3z=1 3x+6y-6z=-3 First write down the augmented matrix: M=[1 1 4 15;2 4 -3 1;3 6 -6 -3] M = 1 1 4 15 2 4 -3 1 3 6 -6 -3 The use the Gaussian algorithm to bring the augmented matrix to row-echelon or reduced row-echelon form. Add -2 times the first row to the second: M(2,:)=M(2,:)-2*M(1,:) M = 1 1 4 15 0 2 -11 -29 3 6 -6 -3 Notice that the third row can be divided by three, so divide the third row by 3: M(3,:)=M(3,:)/3 M = 1 1 4 15 0 2 -11 -29 1 2 -2 -1 Subtract the first row from the third: M(3,:)=M(3,:)-M(1,:) M = 1 1 4 15 0 2 -11 -29 0 1 -6 -16 Switch the second and third rows: M([2,3],:)=M([3,2],:) M = 1 1 4 15 0 1 -6 -16 0 2 -11 -29 Add -2 times the second row to the third one: M(3,:)=M(3,:)-2*M(2,:) M = 1 1 4 15 0 1 -6 -16 0 0 1 3 Now you have the row-echelon form. From here you can get the solution by "back substitution": From the last row you have that z=3. Second row says that y-6z=-16, so y=2. First row says that x+y+4z=15, so x=1. Instead of the "back substitution" you can work more with the matrix to get the reduced row-echelon form. Subtract the second row from the first one: M(1,:)=M(1,:)-M(2,:) M = 1 0 10 31 0 1 -6 -16 0 0 1 3 Add -10 times the third row the the first row: M(1,:)=M(1,:)-10*M(3,:) M = 1 0 0 1 0 1 -6 -16 0 0 1 3 Add 6 times the third row to the second: M(2,:)=M(2,:)+6*M(3,:) M = 1 0 0 1 0 1 0 2 0 0 1 3 Now you have the reduced row-echelon form. From this you can easily see the solutions. The first row says x=1, the second row says y=2, and the third row says z=3. So the solution of the system is: x=1, y=2, z=3. You can also write down the solution in the vector form: (1,2,3). Notice that the first number is the value of x, the second is the value of y, and the third is the value of z. Keep the order of the variables! MATLAB can give you the reduced row-echelon form in one step, let's see how. Enter the augmented matrix (same as above): M=[1 1 4 15;2 4 -3 1;3 6 -6 -3] M = 1 1 4 15 2 4 -3 1 3 6 -6 -3 Ask MATLAB to find the reduced row-echelon form: rref(M) ans = 1 0 0 1 0 1 0 2 0 0 1 3 That's it. The solution of the system is (1,2,3). **************************************************************************************************** Example 2: Solve the system of linear equations over the real numbers: 2y+3z-v=1 2x+6y-4z+10v=8 3x+5y-12z+17v=7 Enter the augmented matrix: M=[0 2 3 -1 1;2 6 -4 10 8;3 5 -12 17 7] M = 0 2 3 -1 1 2 6 -4 10 8 3 5 -12 17 7 Notice that the second row can be divided by 2, so divide the second row by 2: M(2,:)=M(2,:)/2 M = 0 2 3 -1 1 1 3 -2 5 4 3 5 -12 17 7 Since the second row starts with 1, which could be a convenient pivot point, switch the first and second rows: M([1,2],:)=M([2,1],:) M = 1 3 -2 5 4 0 2 3 -1 1 3 5 -12 17 7 Subtract 3 times the first row from the third: M(3,:)=M(3,:)-3*M(1,:) M = 1 3 -2 5 4 0 2 3 -1 1 0 -4 -6 2 -5 Divide the second row by two, to get a 1 for you pivot point (this step is optional): M(2,:)=M(2,:)/2 M = 1.0000 3.0000 -2.0000 5.0000 4.0000 0 1.0000 1.5000 -0.5000 0.5000 0 -4.0000 -6.0000 2.0000 -5.0000 There are fractions in this matrix, and MATLAB shows you the numbers up to the 4th decimal place. If you would rather see the matrix in rational form, then type: format rat M M = 1 3 -2 5 4 0 1 3/2 -1/2 1/2 0 -4 -6 2 -5 However, we note here, that MATLAB uses rationales to APPROXIMATE the exact value. Sometimes it is a problem, please see Example 1 in the on-line MATLAB manual for further explanation on this. Add 4 times the second row to the third: M(3,:)=M(3,:)+4*M(2,:) M = 1 3 -2 5 4 0 1 3/2 -1/2 1/2 0 0 0 0 -3 Look at the last row. That says: 0*x+0*y+0*z+0*v=-3. There are no values for (x,y,z,v) for which this can be true. So the system HAS NO SOLUTION. The system is inconsistent. Again we could have used MATLAB build-in function to get the reduced row-echelon form in one step: Enter the augmented matrix: M=[0 2 3 -1 1;2 6 -4 10 8;3 5 -12 17 7] M = 0 2 3 -1 1 2 6 -4 10 8 3 5 -12 17 7 Get the reduced row-echelon form: rref(M) ans = 1 0 -13/2 13/2 0 0 1 3/2 -1/2 0 0 0 0 0 1 Look at the last row and conclude that the system is inconsistent. ********************************************************************************************************* Example 3: Find all real solutions (p,q,r,s) of the system: p+2r=0 2p-2q+4r-3s=-1 q+3s=5 2p+8q+4r+15s=13 Enter the augmented matrix: M=[1 0 2 0 0;2 -2 4 -3 -1;0 1 0 3 5;2 8 4 15 13] M = 1 0 2 0 0 2 -2 4 -3 -1 0 1 0 3 5 2 8 4 15 13 Bring to reduced row-echelon form: rref(M) ans = 1 0 2 0 0 0 1 0 0 -4 0 0 0 1 3 0 0 0 0 0 This means: p+2r=0, q=-4 and s=3. There are only three equations, but we have 4 unknowns. That means the fourth unknown is free, that can be anything. How to give the solutions? The unknown that has a leading 1 in its column is determined by the row of its leading 1. The unknown that has no leading 1 in its column is a free variable. That is, p is determined by the first row: p+2r=0, or if you rearrange this: p=-2r. q has a leading 1, so it is determined by the second row: q=-4. r has no leading 1, so it is a free variable. s has a leading 1, so it is determined by the third row: s=3. So the solutions are: p=-2r, q=-4, r, s=3. Writing this down with the vector notation, the solutions are: (-2r,-4,r,3) The system has infinitely many solutions. ************************************************************************************************** Example 4: For which value(s) of the constant k does the system x+(k-4)y=k+3 kx+(2k-3)y=2 have (a) no solution (b) exactly 1 solution (c) infinitely many solutions over the field of real numbers? First we have to teach MATLAB that "k" is a parameter (a symbolic variable): syms k M=[1 k-4 k+3;-k 2*k-3 2] M = [ 1, k-4, k+3] [ -k, 2*k-3, 2] Add k times the first row to the second row: M(2,:)=M(2,:)+k*M(1,:) M = [ 1, k-4, k+3] [ 0, 2*k-3+k*(k-4), 2+k*(k+3)] Factor the nonzero terms in the last row: M(2,2)=factor(M(2,2)); M(2,3)=factor(M(2,3)) M = [ 1, k-4, k+3] [ 0, (k+1)*(k-3), (k+2)*(k+1)] (a) The system has no solution if the last row becomes: 0,0,nonzero. So if (k+1)*(k-3)=0 but (k+2)*(k+1) is not zero. This happens if k=3. (b) The system has exactly one solution, if the second row also has a leading one, that is (k+1)*(k-3) is not zero. Therefore the system has exactly one solution if k is neither equal to -1 nor equal to 3. Notice that (k+2)*(k+1) can be anything, zero or not zero, you do still get exactly one solution. (c) The system has infinitely many solutions, if the second row has no leading and the system has solution(s). That is when both (k+1)*(k-3) and (k+2)*(k+1) are equal to 0. This happens when k=-1. Remark: I do NOT suggest using rref(M) for this problem. Try it! How would you answer the questions from that form? Why don't you get the same answer for part (c)? You will not be able to answer part (c) correctly, because MATLAB (and most of the calculators) will divide the second row by (k+1). You know that you cannot divide by (k+1) if that is zero, MATLAB just assumes that is not zero. Which is not correct. *****************************************************************************************************