The required package

To perform linear equations in R, we need to install a package called matlib. To install the package, write the command below:

#install.packages("matlib")

Load the package

library(matlib)

Equations in two unknowns

Suppose we have a linear system,

\[ \begin{alignat*}{7} 5x &&\; - \;&& 4y &&\; = \;&& -10 \\ -x &&\; + \;&& y &&\; = \;&& 2 \end{alignat*} \] that can be represented in a matrix form

\[ \begin{bmatrix} 5 & -4 \\ -1 & 1 \end{bmatrix} \begin{bmatrix} x_{1} \\ x_{2} \end{bmatrix} = \begin{bmatrix} -10 \\ 2 \end{bmatrix} \] that is, \[Ax=b\], and we have \[ x = A^{-1}b\]

Create and show the equation

A <- matrix(c(5, -4, -1, 1), 2, 2, TRUE)
b <- c(-10, 2)
showEqn(A, b)
##  5*x1 - 4*x2  =  -10 
## -1*x1 + 1*x2  =    2

Find solution

Solve(A,b)
## x1    =  -2 
##   x2  =   0

Plot the equation

Use plotEqn() for equations with two-variable equations and use plotEqn3d for three-variable equations.

plotEqn(A,b)
## 5*x[1] - 4*x[2]  =  -10 
##  -x[1]   + x[2]  =    2

Reduced echelon form

echelon(A,b)
##      [,1] [,2] [,3]
## [1,]    1    0   -2
## [2,]    0    1    0
echelon(A, b, verbose=TRUE, fractions=TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3]
## [1,]   5   -4  -10 
## [2,]  -1    1    2 
## 
## row: 1 
## 
##  multiply row 1 by 1/5 
##      [,1] [,2] [,3]
## [1,]    1 -4/5   -2
## [2,]   -1    1    2
## 
##  multiply row 1 by 1 and add to row 2 
##      [,1] [,2] [,3]
## [1,]    1 -4/5   -2
## [2,]    0  1/5    0
## 
## row: 2 
## 
##  multiply row 2 by 5 
##      [,1] [,2] [,3]
## [1,]    1 -4/5   -2
## [2,]    0    1    0
## 
##  multiply row 2 by 4/5 and add to row 1 
##      [,1] [,2] [,3]
## [1,]  1    0   -2  
## [2,]  0    1    0
echelon(A,b, verbose=TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3]
## [1,]    5   -4  -10
## [2,]   -1    1    2
## 
## row: 1 
## 
##  multiply row 1 by 0.2 
##      [,1] [,2] [,3]
## [1,]    1 -0.8   -2
## [2,]   -1  1.0    2
## 
##  multiply row 1 by 1 and add to row 2 
##      [,1] [,2] [,3]
## [1,]    1 -0.8   -2
## [2,]    0  0.2    0
## 
## row: 2 
## 
##  multiply row 2 by 5 
##      [,1] [,2] [,3]
## [1,]    1 -0.8   -2
## [2,]    0  1.0    0
## 
##  multiply row 2 by 0.8 and add to row 1 
##      [,1] [,2] [,3]
## [1,]    1    0   -2
## [2,]    0    1    0