Learn R Programming

GE (version 0.3.8)

iterate: Iteration Function

Description

Iteration function

Usage

iterate(x, f, times = 100, tol = NA, ...)

Value

A matrix consisting of state vectors.

Arguments

x

the initial state vector. If x has a name attribute, the names will be used to label the output matrix.

f

a user-supplied function that computes the values of the next time.

times

the iteration times.

tol

the tolerance for stopping calculation. If the canberra distance of the last two state vectors is less than tol the calculation will stop.

...

optional arguments passed to the f function.

Examples

Run this code
# \donttest{
x <- c(1, 2)
f <- function(x, a) prop.table(c(sum(x), a * prod(x)^(1 / 2)))
iterate(x, f, 100, a = 3)
iterate(x, f, 100, tol = 1e-5, a = 3)

#### Heron's method for finding square roots
x <- 1
f <- function(x, n) (x + n / x) / 2
iterate(x, f, 10, n = 5)

#### Find a root of the equation x^3-x-1==0.
x <- 1.5
f <- function(x) (x + 1)^(1 / 3)
iterate(x, f, 10)

####
x <- c(1, 2, 3)
f <- function(x) {
  n <- length(x)
  sigma <- seq(-1, 1, length.out = n)
  result <- rep(NA, n)
  for (k in 1:n) result[k] <- CES(sigma[k], 1, rep(1 / n, n), x, rep(1 / n, n))
  prop.table(result)
}
iterate(x, f, 100)
# }

Run the code above in your browser using DataLab