Use polynomial as function
I'm pretty new to Mathematica but I'm pretty sure there's an easy way to do it, yet I can't figure it out: if I create a polynomial using InterpolatingPolynomial or similar functions and assign it to a varia开发者_JAVA百科ble (let's call it Poly), how can I transform it in a function callable via
Poly[5]
to obtain the value of the polynomial at x=5? I know I can use
Poly /. x->5
for this, but for what I'm doing next I really need Poly to be a callable function.
Here is another way:
fPoly = Function[x, Evaluate[Poly]]
Evaluate[...] is required since Function[...] "holds" its arguments. Alternatively, you can use
fPoly = Function @@ {x, Poly}
In this latter approach, the head Function isn't applied until after Poly evaluates.
Found out, it was actually really easy:
fPoly = Function[k, Poly /. x -> k];
If you have:
k = InterpolatingPolynomial[{{-1, 4}, {0, 2}, {1, 6}}, u]
Then
poly[x_] := k /. u -> x
精彩评论