The following file has been edited and some comments have been added. The goal is to find all the solutions (if any) of a system of linear equations of the form ___________ | A*X = b | ----------- We are going to try with three methods: 1.- X = A\b This method only gives one result. This result maybe a solution or something that is not a solution but "close enough" (the so called "least square approximation"). 2.- X = inv(A)*b This requires A to be a square, invertible matrix. 3.- rref(c) or Here c = [A b] is the augmented matrix. rrefmovie(c) The output of this command is a "reduced row echelon form" of the augmented matrix "c". It is up to you then to find the solutions (if any) of the system. ****************************************************************************** < M A T L A B (R) > (c) Copyright 1984-94 The MathWorks, Inc. All Rights Reserved Version 4.2c Nov 23 1994 matCommands to get started: intro, demo, help help Commands for more information: help, whatsnew, info, subscribe ***************** * First Example * ***************** In this case the coefficient matrix A is a 3x3 invertible matrix. All the methods described above will work. >> A=[1 1 -1; -1 1 -2; 2 -4 8] A = 1 1 -1 -1 1 -2 2 -4 8 >> b=[4;-3;6] b = 4 -3 6 >> x=A\b x = 3.0000 2.0000 1.0000 >> A*x ans = 4 -3 6 >> x=inv(A)*b x = 3.0000 2.0000 1.0000 >> c=[A b] c = 1 1 -1 4 -1 1 -2 -3 2 -4 8 6 >> rref(c) ans = 1 0 0 3 0 1 0 2 0 0 1 1 ****************** * Second Example * ****************** In this case the coefficient matrix A is a 4x3 matrix. The first method only gives one solution, while the second one cannot work because the matrix is not a square matrix. Finally the Gaussian elimination method will allow us to determine all the solutions (there are an infinite number) >> A=[0 3 -5; 1 1 -3; -2 1 1; -1 2 -2] A = 0 3 -5 1 1 -3 -2 1 1 -1 2 -2 >> b=[13;6;1;7] b = 13 6 1 7 >> x=A\b Warning: Rank deficient, rank = 2 tol = 5.5467e-15 x = -1.8000 0 -2.6000 >> A*x ans = 13.0000 6.0000 1.0000 7.0000 >> x=inv(A)*b ??? Error using ==> inv Matrix must be square. >> c=[A b] c = 0 3 -5 13 1 1 -3 6 -2 1 1 1 -1 2 -2 7 >> rref(c) ans = 1.0000 0 -1.3333 1.6667 0 1.0000 -1.6667 4.3333 0 0 0 0 0 0 0 0 >> format rat >> rref(c) ans = _____ | 1 | 0 -4/3 5/3 |___| _____ 0 | 1 | -5/3 13/3 |___| 0 0 0 0 0 0 0 0 a) Notice that "format rat" allows us to operate with rational numbers and not with decimal numbers!! b) The system is consistent; moreover since there are no leading ones on the third column, we know that the variable "z" is the "free variable" and we also know that we can express "x" and "y" in terms of "z". x = 5/3 + 4/3 z y = 13/3 + 5/3 z z = free variable c) We can also write the general solution in vector form [ x ] [ 5/3 ] [ 4/3 ] [ ] [ ] [ ] [ y ] = X_p + z*X_0 = [ 13/3 ] + z*[ 5/3 ] [ ] [ ] [ ] [ z ] [ 0 ] [ 1 ] Also, note that X_p is a particular solution of the system AX=b, while z*X_0 describes all the solutions of the homogeneous system AX=0. ***************** * Third Example * ***************** This example is exactly the same as the second one except for the column vector "b". In this case the system turns out to be inconsistent: hence there are no solutions. You will note however that the command "A\b" will produce a 3x1 vector. When we check if it is a solution we note that it is not. What is it then?? It is was the computer thinks is the best approximation of a solution (the so called "least square approximation"). >> A=[0 3 -5; 1 1 -3; -2 1 1; -1 2 -2] A = 0 3 -5 1 1 -3 -2 1 1 -1 2 -2 >> b=[13;6;1;8] b = 13 6 1 8 >> x=A\b Warning: Rank deficient, rank = 2 tol = 5.5467e-15 x = -2 0 -8/3 >> A*x ans = 40/3 6 4/3 22/3 >> A*x-b ans = 1/3 0 1/3 -2/3 >> c=[A b] c = 0 3 -5 13 1 1 -3 6 -2 1 1 1 -1 2 -2 8 >> rref(c) ans = 1 0 -4/3 0 0 1 -5/3 0 0 0 0 1 0 0 0 0 ****************** * Fourth Example * ****************** In this case the coefficient matrix A is a 4x5 matrix. The first method only gives a result whose is not a solution, while the second one cannot work because the matrix is not a square matrix. Finally the Gaussian elimination method will allow us to determine all the solutions (there are an infinite number) >> A=[1 1 -1 0 1; -1 -1 2 -1 1; 2 2 -3 3 1; 1 1 0 -3 2] A = 1 1 -1 0 1 -1 -1 2 -1 1 2 2 -3 3 1 1 1 0 -3 2 >> b=[1; 2; 0; 3] b = 1 2 0 3 >> x=A\b Warning: Rank deficient, rank = 3 tol = 4.8393e-15 x = 0 0 2/7 -1/7 9/7 >> A*x-b ans = -1/4503599627370496 -1/1125899906842624 -1/2251799813685248 -1/2251799813685248 >> c=[A b] c = 1 1 -1 0 1 1 -1 -1 2 -1 1 2 2 2 -3 3 1 0 1 1 0 -3 2 3 >> rrefmovie(c) Original matrix A = 1 1 -1 0 1 1 -1 -1 2 -1 1 2 2 2 -3 3 1 0 1 1 0 -3 2 3 Press any key to continue. . . swap rows 1 and 3 A = 2 2 -3 3 1 0 -1 -1 2 -1 1 2 1 1 -1 0 1 1 1 1 0 -3 2 3 Press any key to continue. . . pivot = A(1,1) A = 1 1 -3/2 3/2 1/2 0 -1 -1 2 -1 1 2 1 1 -1 0 1 1 1 1 0 -3 2 3 eliminate in column 1 A = 1 1 -3/2 3/2 1/2 0 0 0 1/2 1/2 3/2 2 1 1 -1 0 1 1 1 1 0 -3 2 3 A = 1 1 -3/2 3/2 1/2 0 0 0 1/2 1/2 3/2 2 0 0 1/2 -3/2 1/2 1 1 1 0 -3 2 3 A = 1 1 -3/2 3/2 1/2 0 0 0 1/2 1/2 3/2 2 0 0 1/2 -3/2 1/2 1 0 0 3/2 -9/2 3/2 3 Press any key to continue. . . column 2 is negligible A = 1 1 -3/2 3/2 1/2 0 0 0 1/2 1/2 3/2 2 0 0 1/2 -3/2 1/2 1 0 0 3/2 -9/2 3/2 3 Press any key to continue. . . swap rows 2 and 4 A = 1 1 -3/2 3/2 1/2 0 0 0 3/2 -9/2 3/2 3 0 0 1/2 -3/2 1/2 1 0 0 1/2 1/2 3/2 2 Press any key to continue. . . pivot = A(2,3) A = 1 1 -3/2 3/2 1/2 0 0 0 1 -3 1 2 0 0 1/2 -3/2 1/2 1 0 0 1/2 1/2 3/2 2 eliminate in column 3 A = 1 1 0 -3 2 3 0 0 1 -3 1 2 0 0 1/2 -3/2 1/2 1 0 0 1/2 1/2 3/2 2 A = 1 1 0 -3 2 3 0 0 1 -3 1 2 0 0 0 0 0 0 0 0 1/2 1/2 3/2 2 A = 1 1 0 -3 2 3 0 0 1 -3 1 2 0 0 0 0 0 0 0 0 0 2 1 1 Press any key to continue. . . swap rows 3 and 4 A = 1 1 0 -3 2 3 0 0 1 -3 1 2 0 0 0 2 1 1 0 0 0 0 0 0 Press any key to continue. . . pivot = A(3,4) A = 1 1 0 -3 2 3 0 0 1 -3 1 2 0 0 0 1 1/2 1/2 0 0 0 0 0 0 eliminate in column 4 A = 1 1 0 0 7/2 9/2 0 0 1 -3 1 2 0 0 0 1 1/2 1/2 0 0 0 0 0 0 A = 1 1 0 0 7/2 9/2 0 0 1 0 5/2 7/2 0 0 0 1 1/2 1/2 0 0 0 0 0 0 A = 1 1 0 0 7/2 9/2 0 0 1 0 5/2 7/2 0 0 0 1 1/2 1/2 0 0 0 0 0 0 Press any key to continue. . . column 5 is negligible A = 1 1 0 0 7/2 9/2 0 0 1 0 5/2 7/2 0 0 0 1 1/2 1/2 0 0 0 0 0 0 Press any key to continue. . . column 6 is negligible A = _____ | 1 | 1 0 0 7/2 9/2 |___| _____ 0 0 | 1 | 0 5/2 7/2 |___| _____ 0 0 0 | 1 | 1/2 1/2 |___| 0 0 0 0 0 0 a) The system is consistent; moreover since there are no leading ones on the second and fifth column, we know that the variable "x_2" and "x_5" are the "free variables" and we also know that we can express "x_1", "x_3", and "x_4" in terms of "x_2" and "x_5". x_1 = 9/2 - x_2 - 7/2 x_5 x_2 = free variable x_3 = 7/2 - 5/2 x_5 x_4 = 1/2 - 1/2 x_5 x_5 = free variable b) We can also write the general solution in vector form [ x_1 ] [ 9/2 ] [ -1 ] [ -7/2 ] [ ] [ ] [ ] [ ] [ x_2 ] [ 0 ] [ 1 ] [ 0 ] [ ] [ ] [ ] [ ] [ x_3 ] = X_p + x_2*X_2 + x_5*X_5 = [ 7/2 ] + x_2*[ 0 ] + x_5*[ -5/2 ] [ ] [ ] [ ] [ ] [ x_4 ] [ 1/2 ] [ 0 ] [ -1/2 ] [ ] [ ] [ ] [ ] [ x_5 ] [ 0 ] [ 0 ] [ 1 ] Also, note that X_p is a particular solution of the system AX=b, while x_2*X_2+x_5*X_5 describes all the solutions of the homogeneous system AX=0.