bernstein: Bernstein Polynomials

View source: R/bernstein.R

bernsteinR Documentation

Bernstein Polynomials

Description

Bernstein base polynomials and approximations.

Usage

bernstein(f, n, x)

bernsteinb(k, n, x)

Arguments

f

function to be approximated by Bernstein polynomials.

k

integer between 0 and n, the k-th Bernstein polynomial of order n.

n

order of the Bernstein polynomial(s).

x

numeric scalar or vector where the Bernstein polynomials will be calculated.

Details

The Bernstein basis polynomials B_{k,n}(x) are defined as

B_{k,n}(x) = {{n}\choose{k}} x^k (1-x)^{n-k}

and form a basis for the vector space of polynomials of degree n over the interval [0,1].

bernstein(f, n, x) computes the approximation of function f through Bernstein polynomials of degree n, resp. computes the value of this approximation at x. The function is vectorized and applies a brute force calculation.

But if x is a scalar, the value will be calculated using De Casteljau's algorithm for higher accuracy. For bigger n the binomial coefficients may be in for problems.

Value

Returns a scalar or vector of function values.

References

See https://en.wikipedia.org/wiki/Bernstein_polynomial

Examples

## Example
f <- function(x) sin(2*pi*x)
xs <- linspace(0, 1)
ys <- f(xs)
## Not run: 
plot(xs, ys, type='l', col="blue",
     main="Bernstein Polynomials")
grid()
b10  <- bernstein(f,  10, xs)
b100 <- bernstein(f, 100, xs)
lines(xs, b10,  col="magenta")
lines(xs, b100, col="red") 
## End(Not run)

# Bernstein basis polynomials
## Not run: 
xs <- linspace(0, 1)
plot(c(0,1), c(0,1), type='n',
     main="Bernstein Basis Polynomials")
grid()
n = 10
for (i in 0:n) {
    bs <- bernsteinb(i, n, xs)
    lines(xs, bs, col=i+1)
} 
## End(Not run)

pracma documentation built on Nov. 10, 2023, 1:14 a.m.