A First Course in Linear Algebra, Section SSLE: Solving Systems of Linear Equations
\(x + y = 5\)
\(2x - y = 3\)
solve function# Create a matrix with the coefficients of the system
a <- matrix(c(1, 2, 1, -1), nrow=2, ncol=2)
a
## [,1] [,2]
## [1,] 1 1
## [2,] 2 -1
# Create a matrix with the right-hand side of the system
b <- matrix(c(5, 3), nrow=2, ncol=1)
b
## [,1]
## [1,] 5
## [2,] 3
# Solve for the unknown variables
s <- solve(a,b)
s
## [,1]
## [1,] 2.666667
## [2,] 2.333333
solve function, we find that \(x \approx 2.67\), or \(\frac{8}{3}\), and \(y \approx 2.33\), or \(\frac{7}{3}\).# Assign x and y to the output of the solve function
x <- s[1]
y <- s[2]
x
## [1] 2.666667
y
## [1] 2.333333
# Equation 1
x + y
## [1] 5
# Equation 2
(2*x) - y
## [1] 3