1

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
arax
  • 1,831
  • 12
  • 16
  • 2
    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: http://mathematica.stackexchange.com/q/11772/193 – Dr. belisarius Jun 17 '14 at 13:41

3 Answers3

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

enter image description here

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[6]
#1 - #1^3/6 + #1^5/120 &
george2079
  • 38,913
  • 1
  • 43
  • 110
  • 1
    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 -> #)]] & – Michael E2 Jun 17 '14 at 15:25
  • good catch, thanks – george2079 Jun 17 '14 at 15:48
2
    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?

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
2

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

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198