Learn R Programming

comphy (version 1.0.5)

solve_tridiag: Tridiagonal linear system

Description

Solution of a system of \(n\) equations in \(n\) unknowns, where the coefficients form a tridiagonal matrix.

Usage

solve_tridiag(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. Matrix \(A\) is a tridiagonal matrix. This means that \(A\) is a sparse matrix with non-zero elements on the main diagonal and the two diagonals adjacent to the main diagonal. The special form of the matrix of coefficients makes it possible to solve the related system using a fast algorithm, here the Thomas algorithm.

Examples

Run this code
# System of four equations in four unknowns
#
# 2x_1 +  x_2               = 1
# 2x_1 + 3x_2 +  x_3        = 2
#         x_2 + 4x_3 + 2x_4 = 3
#                x_3 + 3x_4 = 4

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

# Solution via Thomas algorithm
x <- solve_tridiag(M)
print(x)

Run the code above in your browser using DataLab