bernstein(f,n,t) with a function handle f returns the nth-order Bernstein polynomial symsum(nchoosek(n,k)*t^k*(1-t)^(n-k)*f(k/n),k,0,n), evaluated at the point t. This polynomial approximates the function f over the interval [0,1].
bernstein(g,n,t) with a symbolic expression or function g returns the nth-order Bernstein polynomial, evaluated at the point t. This syntax regards g as a univariate function of the variable determined by symvar(g,1).
If any argument is symbolic, bernstein converts all arguments except a function handle to symbolic, and converts a function handle's results to symbolic.
Approximate the sine function by the 10th- and 100th-degree Bernstein polynomials:
syms t b10 = bernstein(@(t) sin(2*pi*t), 10, t); b100 = bernstein(@(t) sin(2*pi*t), 100, t);
Plot sin(2*pi*t) and its approximations:
pr = ezplot(sin(2*pi*t),[0,1]); hold on pg = ezplot(b10,[0,1]); pb = ezplot(b100,[0,1]); set(pr,'Color','red') set(pg,'Color','green') set(pb,'Color','blue') legend('sine function','10th-degree polynomial',... '100th-degree polynomial') title('Bernstein polynomials') hold off
Approximate the exponential function by the second-order Bernstein polynomial in the variable t:
syms x t; bernstein(exp(x), 2, t)
ans = (t - 1)^2 + t^2*exp(1) - 2*t*exp(1/2)*(t - 1)
Approximate the multivariate exponential function. When you approximate a multivariate function, bernstein regards it as a univariate function of the default variable determined by symvar. The default variable for the expression y*exp(x*y) is x:
syms x y t; symvar(y*exp(x*y), 1)
ans = x
bernstein treats this expression as a univariate function of x:
bernstein(y*exp(x*y), 2, t)
ans = y*(t - 1)^2 + t^2*y*exp(y) - 2*t*y*exp(y/2)*(t - 1)
To treat y*exp(x*y) as a function of the variable y, specify the variable explicitly:
bernstein(y*exp(x*y), y, 2, t)
ans = t^2*exp(x) - t*exp(x/2)*(t - 1)
Approximate function f representing a linear ramp by the fifth-order Bernstein polynomials in the variable t:
syms f(t); f(t) = triangularPulse(1/4, 3/4, Inf, t); p = bernstein(f, 5, t)
p = 7*t^3*(t - 1)^2 - 3*t^2*(t - 1)^3 - 5*t^4*(t - 1) + t^5
Simplify the result:
ans = -t^2*(2*t - 3)
When you simplify a high-order symbolic Bernstein polynomial, the result often cannot be evaluated in a numerically stable way.
Approximate this rectangular pulse function by the 100th-degree Bernstein polynomial, and then simplify the result:
f = @(x)rectangularPulse(1/4,3/4,x); b1 = bernstein(f, 100, sym('t')); b2 = simplify(b1);
Convert the polynomial b1 and the simplified polynomial b2 to MATLAB® functions:
f1 = matlabFunction(b1); f2 = matlabFunction(b2);
Compare the plot of the original rectangular pulse function, its numerically stable Bernstein representation f1, and its simplified version f2. The simplified version is not numerically stable.
t = 0:0.001:1; plot(t, f(t), t, f1(t), t, f2(t)) hold on legend('original function','Bernstein polynomial',... 'simplified Bernstein polynomial') hold off
Function to be approximated by a polynomial, specified as a function handle. f must accept one scalar input argument and return a scalar value.
Function to be approximated by a polynomial, specified as a symbolic expression or function.
Bernstein polynomial order, specified as a nonnegative number.
Evaluation point, specified as a number, symbolic number, variable, expression, or function. If t is a symbolic function, the evaluation point is the mathematical expression that defines t. To extract the mathematical expression defining t, bernstein uses formula(t).
Free variable, specified as a symbolic variable.
A Bernstein polynomial is a linear combination of Bernstein basis polynomials.
A Bernstein polynomial of degree n is defined as follows:
are the Bernstein basis polynomials, and is a binomial coefficient.
The coefficients are called Bernstein coefficients or Bezier coefficients.
If f is a continuous function on the interval [0, 1] and
is the approximating Bernstein polynomial, then
uniformly in t on the interval [0, 1].