Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
2  
f@x_:=Sin@x; Plot[{f[x], ff[#, x] & /@ Range@5}, {x, 0, Pi}, Evaluated -> True] –  belisarius Jun 17 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! –  Alex Su Jun 17 at 13:09
    
This may be useful: mathematica.stackexchange.com/q/11772/193 –  belisarius Jun 17 at 13:41

3 Answers 3

up vote 4 down vote accepted
 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 &
share|improve this answer
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 at 15:25
    
good catch, thanks –  george2079 Jun 17 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?

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

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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