Skip to contents

Solve the equation system \(Ax = b\), given the coefficient matrix \(A\) and right-hand side vector \(b\), using link{gaussianElimination}. Display the solutions using showEqn.

Usage

Solve(
  A,
  b = rep(0, nrow(A)),
  vars,
  verbose = FALSE,
  simplify = TRUE,
  fractions = FALSE,
  ...
)

Arguments

A,

the matrix of coefficients of a system of linear equations

b,

the vector of constants on the right hand side of the equations. The default is a vector of zeros, giving the homogeneous equations \(Ax = 0\).

vars

a numeric or character vector of names of the variables. If supplied, the length must be equal to the number of unknowns in the equations. The default is paste0("x", 1:ncol(A).

verbose,

logical; show the steps of the Gaussian elimination algorithm?

simplify

logical; try to simplify the equations?

fractions

logical; express numbers as rational fractions, 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)).

...,

arguments to be passed to link{gaussianElimination} and showEqn

Value

the function is used primarily for its side effect of printing the solution in a readable form, but it invisibly returns the solution as a character vector

Details

This function mimics the base function solve when supplied with two arguments, (A, b), but gives a prettier result, as a set of equations for the solution. The call solve(A) with a single argument overloads this, returning the inverse of the matrix A. For that sense, use the function inv instead.

Author

John Fox

Examples

  A1 <- matrix(c(2, 1, -1,
               -3, -1, 2,
               -2,  1, 2), 3, 3, byrow=TRUE)
  b1 <- c(8, -11, -3)
  Solve(A1, b1) # unique solution
#> x1      =   2 
#>   x2    =   3 
#>     x3  =  -1 

  A2 <- matrix(1:9, 3, 3)
  b2 <- 1:3
  Solve(A2,  b2, fractions=TRUE) # underdetermined
#> x1   - 1*x3  =  1 
#>   x2 + 2*x3  =  0 
#>           0  =  0 

  b3 <- c(1, 2, 4)
  Solve(A2, b3, fractions=TRUE) # overdetermined
#> x1   - 1*x3  =   5/3 
#>   x2 + 2*x3  =  -1/6 
#>           0  =  -1/2