cg: Conjugate Gradient algorithm

View source: R/cg.R

cgR Documentation

Conjugate Gradient algorithm

Description

The function solves a symmetric positive definite system by constructing conjugate directions. Similar to Lanczos, R is matrix containing Krylov orthogonal vectors (non-normalized), P is a matrix of conjugate directions, and H is a tridiagonal matrix with Lanczos coefficients.

Usage

cg(A, b, x = rnorm(length(b)), m = length(b), tol = 1e-07)

Arguments

A

A symmetric positive definite matrix for the system Ax = b. No default value.

b

A right hand side vector in Ax = b. No default value.

x

An initial guess which produces the first residual r = b - Ax. Default is a random vector.

m

Maximum number of iterations to be run. Default is length(b).

tol

The relative tolerance for residual norms. Default is 1e-7.

Examples

A = matrix(rnorm(100^2), 100)
A = t(A)%*%A
b = rnorm(100)
res = cg(A, b, 80)
plot(solve(A, b), t="l")
lines(res$x, col="red")

abylayzhumekenov/lanczos documentation built on April 27, 2022, 9:54 a.m.