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.

  • 2
    $\begingroup$ f@x_:=Sin@x; Plot[{f[x], ff[#, x] & /@ Range@5}, {x, 0, Pi}, Evaluated -> True] $\endgroup$ – Dr. belisarius Jun 17 '14 at 13:01
  • $\begingroup$ @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! $\endgroup$ – arax Jun 17 '14 at 13:09
  • $\begingroup$ This may be useful: mathematica.stackexchange.com/q/11772/193 $\endgroup$ – Dr. belisarius Jun 17 '14 at 13:41
 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] :

     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}]

enter image description here


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

#1 - #1^3/6 + #1^5/120 &
  • 1
    $\begingroup$ I think if you look at ff[1], 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 -> #)]] & $\endgroup$ – Michael E2 Jun 17 '14 at 15:25
  • $\begingroup$ good catch, thanks $\endgroup$ – 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]}]

enter image description here

Is it what you are after?

  • $\begingroup$ 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 $\endgroup$ – arax Jun 17 '14 at 13:01
  • 1
    $\begingroup$ Alex Su Yes, I can. If it does not plot where there is no reason for that, try Evaluate. $\endgroup$ – Alexei Boulbitch Jun 17 '14 at 13:05
  • $\begingroup$ Well it feels like painting the rubik's cube. Fair enough $\endgroup$ – 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}]];

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

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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