1
$\begingroup$

Given a function $f(x) \in C[0,1]$, we define the $n$th Bernstein Operator $T_n$ by

$(T_nf)(t) = \sum_{k=0}^n {{n}\choose{k}}f\left(\frac{k}{n}\right)t^k(1 - t)^{n - k}$.

I'm trying to code this in Mathematica, but I don't see how it can be done to take an unknown function as an argument. My current working code is

b[f[x_]_, n_, t_] := Sum[(n!/(k!*(n - k)!))*f(k/n)*t^k*(1 - t)^(n - k), {k, 0, n}]

but this doesn't seem to work. Can anyone offer a way to take an undefined function as an input?

Edit: Though the code below in the comments works spectacularly, the code also works as-is, so long as one remembers to change $f(k/n)$ to $f[k/n]$, so that Mathematica knows to evaluate $f$ at $k/n$.

$\endgroup$
4
  • 2
    $\begingroup$ bop[n_Integer, f_, t_] := Sum[Binomial[n, k] Function[t, f][k/n] t^k (1 - t)^(n - k), {k, 0, n}], and then try bop[5, Exp[t], t]. $\endgroup$ Commented Mar 28, 2018 at 20:01
  • $\begingroup$ That worked perfectly! Thank you! $\endgroup$ Commented Mar 28, 2018 at 20:11
  • 2
    $\begingroup$ Also for reference: BernsteinBasis[] is built-in, so you can modify my code to shorten it further. $\endgroup$ Commented Mar 28, 2018 at 20:15
  • $\begingroup$ Welcome! To make the most of Mma.SE start by taking the tour now. It will help us to help you if you write an excellent question. Edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$
    – rhermans
    Commented Jun 16, 2018 at 10:44

1 Answer 1

1
$\begingroup$

Using a slightly different notation, consistent with mathworld and literature

ClearAll[BernsteinOperator];
BernsteinOperator[n_Integer][f_, x_Symbol] := Sum[
  Function[x, f][i/n] BernsteinBasis[n, i, x]
  , {i, 0, n}
  ]

PiecewiseExpand[BernsteinOperator[2][a Exp[b x], x]]

Mathematica graphics

Plot[
 Evaluate@{
   Exp[x],
   BernsteinOperator[9][Exp[x], x]
   }
 , {x, -0.5, 1.5}
 , PlotTheme -> "Scientific"
 , PlotStyle -> {Gray, Red}
 ]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.