Learn R Programming

matlib (version 0.9.6)

gaussianElimination: Gaussian Elimination

Description

gaussianElimination demonstrates the algorithm of row reduction used for solving systems of linear equations of the form \(A x = B\). Optional arguments verbose and fractions may be used to see how the algorithm works.

Usage

gaussianElimination(
  A,
  B,
  tol = sqrt(.Machine$double.eps),
  verbose = FALSE,
  latex = FALSE,
  fractions = FALSE
)

# S3 method for enhancedMatrix print(x, ...)

Value

If B is absent, returns the reduced row-echelon form of A. If B is present, returns the reduced row-echelon form of A, with the same operations applied to B.

Arguments

A

coefficient matrix

B

right-hand side vector or matrix. If B is a matrix, the result gives solutions for each column as the right-hand side of the equations with coefficients in A.

tol

tolerance for checking for 0 pivot

verbose

logical; if TRUE, print intermediate steps

latex

logical; if TRUE, and verbose is TRUE, print intermediate steps using LaTeX equation outputs rather than R output

fractions

logical; if TRUE, try to express non-integers as rational numbers, using the fractions function; if you require greater accuracy, you can set the cycles (default 10) and/or max.denominator (default 2000) arguments to fractions as a global option, e.g., options(fractions=list(cycles=100, max.denominator=10^4)).

x

matrix to print

...

arguments to pass down

Author

John Fox

Examples

Run this code
  A <- matrix(c(2, 1, -1,
               -3, -1, 2,
               -2,  1, 2), 3, 3, byrow=TRUE)
  b <- c(8, -11, -3)
  gaussianElimination(A, b)
  gaussianElimination(A, b, verbose=TRUE, fractions=TRUE)
  gaussianElimination(A, b, verbose=TRUE, fractions=TRUE, latex=TRUE)

  # determine whether matrix is solvable
  gaussianElimination(A, numeric(3))

  # find inverse matrix by elimination: A = I -> A^-1 A = A^-1 I -> I = A^-1
  gaussianElimination(A, diag(3))
  inv(A)

  # works for 1-row systems (issue # 30)
  A2 <- matrix(c(1, 1), nrow=1)
  b2 = 2
  gaussianElimination(A2, b2)
  showEqn(A2, b2)
  # plotEqn works for this case
  plotEqn(A2, b2)

Run the code above in your browser using DataLab