Learn R Programming

comphy (version 1.0.5)

gauss_elim: Gaussian Elimination

Description

Solution of a system of \(n\) equations in \(n\) unknowns, using Gaussian elimination.

Usage

gauss_elim(M)

Value

A vector of length \(n\) containing the \(n\)

numeric solutions for the \(n\) unknowns. If the system has no solutions or an infinite number of solutions, the function returns NULL and dumps a warning message.

Arguments

M

The \(n\times (n+1)\) augmented matrix of coefficients corresponding to the system of \(n\) linear equations in \(n\) unknowns, \(Ax=b\).

Details

The linear system to solve is \(Ax=b\), where \(A\) is the \(n\times n\) matrix of coefficients of the \(n\) unknowns in the \(n\times 1\) vector \(x\), and \(b\) is the \(n\times 1\) vector of known numbers. Gaussian elimination consists of a series of so-called row operations that transform \(A\) in an upper-triangular matrix. The system corresponding to the transformed matrix can be solved very quickly.

Examples

Run this code
# System of three equations in three unknowns
#
# 3x_1 + x_2 +  x_3 = 6
#  x_1 - x_2 + 2x_3 = 4
# -x_1 + x_2 +  x_3 = 2

# Augmented matrix M=(A|b)
M <- matrix(c(3,1,-1,1,-1,1,1,2,1,6,4,2),ncol=4)

# Solution via Gauss elimination
x <- gauss_elim(M)
print(x)

Run the code above in your browser using DataLab