Section 13 Linear Systems in R

13.1 Getting started with R

To use RStudio, you have two choices:

  1. Use the cloud version by logging in to Rstudio.macalester.edu. This is the easiest way to use RStudio and works great for our course.

  2. You can also download the free desktop version of RStudio. If you plan to go on to take more MSCS classes, especially in statistics and data science, you may want to use the desktop version. Download the desktop version following the instructions here: rstudio.com/products.

Now, let’s learn how to use R to solve systems of linear equations! Download this Rmd file. First, we will create vectors and matrices Then we will see how to create an augmented matrix and then apply Gaussian Elimination to obtain is reduced row echelon form.

Gaussian elimination is performed by the rref() command. However, this command is not loaded into R by default. So we have have to tell RStudio to use the practical math package, which is known as pracma. So we need to run the following command once at the beginning of our session.

require(pracma)

13.2 Building Vectors and Matrices

A vector in R is a list of data. The simplest way to create a vector is to use the c() command. The letter ‘c’ is short for ‘combine these values into a vector.’ For example, we can make a vector v for the numbers 1,2,3 as follows:

v=c(1,2,3)
v
## [1] 1 2 3

Note that we had to ask R to display the value of v. This is because the assignment of v doesn’t echo the value to the console. But can see the value of v in the Environment tab in the upper right panel of RStudio. For example, run this command and then check to see that the value of v gets updated in the environment.

v=c(1,2,3,4,5,6)

It is interesting to note that c() returns a dimensionless vector. So you can treat a vector c() as either a row or a column when you construct a matrix. For example, suppose that we want to make the matrix \[ A = \begin{bmatrix} 1 & 1 & 1 \\ 2 & 4 & 8 \\ 3 & 9 & 27 \end{bmatrix}. \] We could create this matrix by binding three row vectors:

A = rbind(c(1,1,1), c(2,4,8), c(3,9,27))
A
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    4    8
## [3,]    3    9   27

or we could bind three column vectors:

A = cbind(c(1,2,3), c(1,4,9), c(1,8,27))
A
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    4    8
## [3,]    3    9   27

13.3 Solving a Linear System

Suppose that we want to solve the linear system \[\begin{aligned} x + y + z &= 7 \\ 2x + 4y + 8z &= 6 \\ 3x +9y+27z &=12 \end{aligned}\]

which has coefficient matrix \[ A = \begin{bmatrix} 1 & 1 & 1 \\ 2 & 4 & 8 \\ 3 & 9 & 27 \end{bmatrix}. \] and target (column) vector \[ b = \begin{bmatrix} 4 \\ 6 \\ 12 \end{bmatrix}. \] This is the same matrix A we defined above. Let’s define a vector b and use cbind() to create an augmented matrix which we will name Ab. (We could have just made the full augmented matrix from the start, but using cbind to add a column to a matrix is a skill we will use later in the course!)

A
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    4    8
## [3,]    3    9   27
b = c(4,6,12)
Ab = cbind(A,b)
Ab
##              b
## [1,] 1 1  1  4
## [2,] 2 4  8  6
## [3,] 3 9 27 12

Now we use the rref() command to apply Gaussian Elimination to produce the reduced row echelon form. (And remember: we had to load this function into R by using the require(pracma) command above.)

rref(Ab)
##             b
## [1,] 1 0 0  7
## [2,] 0 1 0 -4
## [3,] 0 0 1  1

We conclude that this is a consistent system no free variables. The unique solution is \[\begin{align} x&=7\\ y&=-4\\ z&=1 \end{align}\]

We can verify that our answer works by multiplying \(A\) by one of the solutions above. Matrix multiplication uses the funny operation %*%.

A %*% c(7,-4,1)
##      [,1]
## [1,]    4
## [2,]    6
## [3,]   12
#A %*% c(1,2,3,0,0)

Which matches our target \[ b = \begin{bmatrix} 4 \\ 6 \\ 12 \end{bmatrix} \] just as we had hoped.

13.4 Solving another Linear System

Now let’s find the solution set for the linear system \[ \begin{array}{rrrrrcr} x_1 & & -x_3 & -x_4 & -x_5 & = & -2 \\ 2x_1 & +x_2 & +2x_3 & -x_4 & -x_5 & = & 4 \\ -x_1 & +x_2 & +x_3 & & & = & 10 \\ x_1 & & -x_3 & -x_4 & -x_5 & = & -2 \\ \end{array} \] which corresponds to augmented matrix \[ \left[ \begin{array}{rrrrr|r} 1 & & -1 & -1 & -1 & -2 \\ 2 & +1 & +2 & -1 & -1 & 4 \\ -1 & +1 & +1 & & & 10 \\ 1 & & -1 & -1 & -1 & -2 \\ \end{array} \right] \]

This time, let’s just construct the augmented matrix direclty.

Then we define the coefficient matrix \(A\). Here we use cbind to combine the vectors into the columns of a matrix named \(A\). You can use rbind if you want to combine the vectors into the rows of a matrix.

Ab = cbind(c(1,2,-1,1),c(0,1,1,0),c(-1,2,1,-1),c(-1,1,0,-1),c(-1,5,0,-1),c(-2,10,4,-2))
Ab
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0   -1   -1   -1   -2
## [2,]    2    1    2    1    5   10
## [3,]   -1    1    1    0    0    4
## [4,]    1    0   -1   -1   -1   -2

And now let’s row reduce to get RREF.

rref(Ab)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0    0    0    1    1
## [2,]    0    1    0   -1   -1    2
## [3,]    0    0    1    1    2    3
## [4,]    0    0    0    0    0    0

So the set of solutions in parametric form is

\[ \begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \end{bmatrix} = \begin{bmatrix} 1 \\ 2 \\ 3 \\ 0 \\ 0 \end{bmatrix} + s \begin{bmatrix} 0 \\ 1 \\ -1 \\ 1 \\ 0 \end{bmatrix} + t \begin{bmatrix} -1 \\ 1 \\ -2 \\ 0 \\ 1 \end{bmatrix} \]

and this is a “plane” in \(\mathbb{R}^5\). It is in \(\mathbb{R}^5\) because these vectors have 5 coordinates. It is a plane because it is spanned by two vectors that are not on the same line.