Learn R Programming

pracma (version 1.8.8)

bernoulli: Bernoulli Numbers and Polynomials

Description

The Bernoulli numbers are a sequence of rational numbers that play an important role for the series expansion of hyperbolic functions, in the Euler-MacLaurin formula, or for certain values of Riemann's function at negative integers.

Usage

bernoulli(n, x)

Arguments

n
the index, a whole number greater or equal to 0.
x
real number or vector of real numbers; if missing, the Bernoulli numbers will be given, otherwise the polynomial.

Value

  • Returns the first n+1 Bernoulli numbers, if x is missing, or the value of the Bernoulli polynomial at point(s) x.

Details

The calculation of the Bernoulli numbers uses the values of the zeta function at negative integers, i.e. $B_n = -n \, zeta(1-n)$. Bernoulli numbers $B_n$ for odd n are 0 except $B_1$ which is set to -0.5 on purpose.

The Bernoulli polynomials can be directly defined as $$B_n(x) = \sum_{k=0}^n {n \choose k} b_{n-k}\, x^k$$ and it is immediately clear that the Bernoulli numbers are then given as $B_n = B_n(0)$.

References

See the entry on Bernoulli numbers in the Wikipedia.

See Also

zeta

Examples

Run this code
bernoulli(10)
# 1.00000000 -0.50000000  0.16666667  0.00000000 -0.03333333
# 0.00000000  0.02380952  0.00000000 -0.03333333  0.00000000  0.07575758
                #
x1 <- linspace(0.3, 0.7, 2)
y1 <- bernoulli(1, x1)
plot(x1, y1, type='l', col='red', lwd=2,
     xlim=c(0.0, 1.0), ylim=c(-0.2, 0.2),
     xlab="", ylab="", main="Bernoulli Polynomials")
grid()
xs <- linspace(0, 1, 51)
lines(xs, bernoulli(2, xs), col="green", lwd=2)
lines(xs, bernoulli(3, xs), col="blue", lwd=2)
lines(xs, bernoulli(4, xs), col="cyan", lwd=2)
lines(xs, bernoulli(5, xs), col="brown", lwd=2)
lines(xs, bernoulli(6, xs), col="magenta", lwd=2)
legend(0.75, 0.2, c("B_1", "B_2", "B_3", "B_4", "B_5", "B_6"),
       col=c("red", "green", "blue", "cyan", "brown", "magenta"),
       lty=1, lwd=2)

Run the code above in your browser using DataLab