# Create polynomials from Series

This question actually doesn't have quite a lot to do with the Series function, but I don't know how to describe my problem. So here's the thing.

I'm trying to plot function f[x] and its Maclaurin expansion together

ff[n_,x_] := Normal@Series[f[x],{x,0,n}]


But when I feed ff with an x, Mathematica always substitute the x in the expression with its actual value, which is not how Series works. So how could I hold the numerical value of x until Series finishes its calculation?

This problem always happens to me, though in different contexts, and I used to find ways around it, but this time I want to solve it in the correct way.

• f@x_:=Sin@x; Plot[{f[x], ff[#, x] & /@ Range@5}, {x, 0, Pi}, Evaluated -> True] – Dr. belisarius Jun 17 '14 at 13:01
• @belisarius it quite confuses me: I did not tell Mathematica to hold anything, why do I have to tell it to evaluate? BTW your expression works well, even without specifically declaring Evaluated to be True, but when I write Plot[ff[1,x], ... ], it doesn't work and I have to add Evaluated -> True. Either some deeper connection is eluding me, or Mathematica works in a truly mysterious way! – arax Jun 17 '14 at 13:09
• This may be useful: mathematica.stackexchange.com/q/11772/193 – Dr. belisarius Jun 17 '14 at 13:41

## 3 Answers

 ff[n_,x_] := Normal@Series[f[x0],{x0,0,n}]  /. x0->x


or this is probably preferred since we dont want to reevaluate the Series for each x:

 f[x_] = Sin[x];
ff[n_] := (Normal@Series[f[x0], {x0, 0, n}] /. x0 -> #) &;


note the usage then becomes f[n][x] :

 Show[{
Plot[Sin[x], {x, 0, 4 Pi}, PlotStyle -> Red],
Plot[ff[#][x] & /@ {1, 2, 3, 5, 10, 20}, {x, 0, 4 Pi}]},
PlotRange -> {-3, 3}] ## edit

as pointed out by @MichaelE2, the above does not in fact save us from revaluation of Series for every x. This does however:

 ff[n_Integer] :=
ff[n] = Evaluate[(Normal@
Series[f[\[FormalX]], {\[FormalX], 0, n}] /. \[FormalX] -> #)] &


Just to avoid completely stealing the code I used a formal character ( esc-\$-x-esc ) rather than block protecting x0

 ff

#1 - #1^3/6 + #1^5/120 &

• I think if you look at ff, you'll see that Series is unevaluated (since Function has the attribute HoldAll). It will still get evaluated for each x. I think you want something more sophisticated, like ff[n_Integer] := ff[n] = Evaluate[Block[{x0}, (Normal@Series[f[x0], {x0, 0, n}] /. x0 -> #)]] & – Michael E2 Jun 17 '14 at 15:25
• good catch, thanks – george2079 Jun 17 '14 at 15:48
    f[x_] := Sin[x];
ff[n_, x_] := Normal[Series[f[x], {x, 0, n}]];
Plot[Evaluate[ff[5, x]], {x, 0, \[Pi]}] Is it what you are after?

• Could you explain to me, or give me some material about why Evaluate could work in this case? I'm afraid the Mathematical manual is not quite clear – arax Jun 17 '14 at 13:01
• Alex Su Yes, I can. If it does not plot where there is no reason for that, try Evaluate. – Alexei Boulbitch Jun 17 '14 at 13:05
• Well it feels like painting the rubik's cube. Fair enough – arax Jun 17 '14 at 13:11

With some "bells and whistles"

f[x_] = Sin[x];

ff[n_, x_] := Normal[Series[f[x], {x, 0, n}]];

Manipulate[
Plot[
Evaluate[Tooltip /@ {f[x], ff[n, x]}],
{x, 0, \[Pi]},
PlotLegends -> {
ToString[f[x], TraditionalForm],
"Polynomial\napproximation"}],
{{n, 5, "Order of\npolynomial"}, 1, 20, 1,
Appearance -> "Labeled"}] 