bernstein | R Documentation |
Bernstein base polynomials and approximations.
bernstein(f, n, x)
bernsteinb(k, n, x)
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. |
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.
Returns a scalar or vector of function values.
See https://en.wikipedia.org/wiki/Bernstein_polynomial
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.