MATLAB 2
SOLVING SYSTEMS OF LINEAR
EQUATIONS
Paul Vaz
NAME__________________________________________________________
In this lab, we learn how to solve systems of linear equations given by AX = B. We begin by introducing the Elementary Row Operations.
Let A denote any matrix. An elementary row operation of type 1 switches 2 rows of A . In Matlab, the row operation can be illustrated in the following example:
>> A = magic(8);
>> A( [1,4], : ) = A( [4,1], : )
An elementary row operation of type 2 multiplies a row of A by a nonzero scalar r.
In Matlab , this is easily done as shown in the following example:
>> A(3, : ) = 10 * A(3, : )
An elementary row operation of type 3 multiplies a row of A by a scalar and adds it to another row
. In Matlab:
>> A(8, : ) = 2 * A(1, : ) + A(8, : )
Returning to the problem of solving a system of equations. Starting with AX = B , we form the augmented matrix [A, B]. [A, B] is the matrix obtained by placing the column vector B at the far right of the matrix A . The strategy is to systematically apply elementary row operations to simplify
[A, B].
The goal of applying the elementary row operations is to bring the matrix into a form where the solutions can be found easily. The form we seek is the reduced row echelon form. A matrix is in reduced row echelon form if
(1) The first nonzero entry in each row is a 1. This is called a leading 1.
(2) The entries above and below in a column where a leading 1 occurs are 0.
(3) The leading 1 in the i -th row is to the left of the leading 1 in the i+1 st row .
(4) Any row which have only 0 entries are at the bottom of the matrix.
Suppose we start with the system
2 x
10 y
12 z
3 w
0
3 x
15 y
18 z
4 w
0 x
4 y
5 z
13 w
0
We will use elementary row operations of type 2 to create leading 1’s and then we will use elementary operations of type 3 to zero out the entries above and below the leading 1’s. The entry which is selected to become the leading is called the pivot . Begin by creating the coefficient matrix:
>> A = [2, -10, 12, 3; 3, -15, 18, 4; 1, -4, 5, 13]
We see that the (1,1) entry (element in first row - first column) is nonzero, so it will be our pivot.
>> A(1, : ) = 1/A(1,1) * A(1, : )
We now have a leading 1, so we use it to zero out the other entries in the first column.
>> A(2, : ) = - A(2,1)*A(1, : ) + A(2, : )
>> A(3, : ) = - A(3,1)*A(1, : ) + A(3, : )
We have created our first leading 1and zeroed out below. Now move to the next row and next column. We see that there is a 0 in the (2,2) position so we cannot use an elementary row operation of type 2 to turn into a leading one. There is a nonzero entry below it in the (3,2) position, so the
(3,2) element will be the pivot. Use an elementary row operation of type 1 to move that nonzero entry into the (2,2) position, then wipe out above and below.
>> A( [2,3], : ) = A( [3,2], : )
>> A(1, : ) = - A(1,2)*A(2, : ) + A(1, : )
We have a leading 1in the second row. Move to the third row and third column. In the (3,3) position there is a 0, but this time there is no nonzero entry below. So we move on to the (3,4) position. There is a nonzero entry. It becomes the pivot. Turn it into a leading one and zero out above.
>> A(3, : ) = 1/A(3,4)*A(3, : )
>> A(2, : ) = - A(2,4)*A(3, : ) + A(2, : )
>> A(1, : ) = - A(1,4)*A(3, : ) + A(1, : )
We are now in reduced row echelon form. Write out the equations which go with the reduced row echelon form.
The variables x, y, w correspond to the leading 1’s. We will call these leading variables.
The variable z which is not a leading variable is called a free variable or a parameter . Solve for the leading variables in terms of the free variable:
You can let z take any value and the values for the leading variables will be determined.
Write 3 solutions:
X
1
X
2
X
3
Use of Matlab function rref :
The Matlab function rref uses elementary row operations to convert a matrix into a reduced row echelon form matrix. You can use this function to reduce the matrix A to the reduced row echelon form:
>> rref(A)
Try some other matrices:
>> rref(magic(4))
>> rref(rand(5))
PROBLEMS:
1. Find a solution to BX = 0 where B = magic(6). Assign random values to the free
variables and write a solution. ( You may use rref)
2.
Let a, b, and c be matrices defined as follows:
a = ones(6); a(: ) = 1: 36 , b =ones(6,1) , c = [0 0 0 0 0 1]’.
Determine if aX = b and aX = c are consistent.