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.

Let us say I want to show how the Taylor's series for Sin behaves:

In[94]:= Plot[Normal[Series[Sin[x], {x, 0, 3}]], {x, -\[Pi], \[Pi]}]

During evaluation of In[94]:= General::ivar: -3.14146 is not a valid variable. >>    
During evaluation of In[94]:= General::ivar: -3.14146 is not a valid variable. >>    
During evaluation of In[94]:= General::ivar: -3.14146 is not a valid variable. >>    
During evaluation of In[94]:= General::stop: Further output of General::ivar will be suppressed during this calculation. >>

However, if I just evaluate

In[95]:= Normal[Series[Sin[x], {x, 0, 3}]]

Out[95]= x - x^3/6

In[96]:= Plot[x - x^3/6, {x, -\[Pi], \[Pi]}]


enter image description here

It works fine. What is going on? I am doing this because I want to insert the plot in a Manipulate and then see how the functions behave with more terms of the series.


Using the answer, I can do

Plot[Evaluate@Normal[Series[Sin[x], {x, 0, 3}]], {x, -\[Pi], \[Pi]}]

However, if I try to compare as in

Plot[{Sin[x], Evaluate@Normal[Series[Sin[x], {x, 0, 3}]]}, {x, -\[Pi], \[Pi]}]

it doesn't work while the explicit Plot[{Sin[x], x - x^3/6}, {x, -\[Pi], \[Pi]}] does.

share|improve this question
add comment

2 Answers

As kguler shows this is an evaluation order problem.

I recommend a different form however:

  {Sin[x], Normal[Series[Sin[x], {x, 0, 3}]]},
  {x, -π, π},
  Evaluated -> True

For Plot the undocumented option Evaluated is superior because the plotting variable (x) is still correctly localized, therefore this method works even if you set a global value e.g. x = 1 before plotting.

You asked why this doesn't work:

Plot[{Sin[x], Evaluate@Normal[Series[Sin[x], {x, 0, 3}]]}, {x, -π, π}]

This fails because as the Evaluate documentation under Possible Issues states:

Evaluate works only on the first level, directly inside a held function:

Hold[f[Evaluate[1 + 2]]]
Out[1]= Hold[f[Evaluate[1 + 2]]]
share|improve this answer
I think the Evaluated option deserves a short entry in our new blog –  belisarius Jun 28 '12 at 11:55
add comment


 Plot[Evaluate@Normal[Series[Sin[x], {x, 0, 3}]], {x, -\[Pi], \[Pi]}]


 Plot[#, {x, -\[Pi], \[Pi]}] &@Normal[Series[Sin[x], {x, 0, 3}]]


 Plot[Normal[Series[Sin[y], {y, 0, 5}]] /. y -> x, {x, -Pi, Pi}]

to force evaluation in the appropriate order.


Plot[Evaluate@{Sin[x], Normal[Series[Sin[x], {x, 0, 3}]]}, {x, -Pi, Pi}]
Plot[{Sin[x], Normal[Series[Sin[x], {x, 0, 3}]]}, {x, -Pi, Pi}, Evaluated->True]
Plot[{Sin[x], #}, {x, -Pi, Pi}] &@Normal[Series[Sin[x], {x, 0, 3}]]
Plot[{Sin[x], Normal[Series[Sin[y], {y, 0, 3}]] /. y -> x}, {x, -Pi,  Pi}]

all give

enter image description here

EDIT 2: The error message

 General::ivar: "-3.14146 is not a valid variable."

suggests why Plot[Normal[Series[Sin[x], {x, 0, 5}]], {x, -Pi, Pi}] does not work. Namely, as Plot plugs numerical values x0 from the range (-Pi,Pi) in its first argument, the expression Series[Sin[x0], {x0, 0, 5}]] becomes an invalid expression.

EDIT 3: The option setting Evaluated->True in Mr.Wizard is most straightforward approach. The default setting for that option

 (* ==> Automatic *)

By making the default setting for this option True using

SetOptions[Plot, Evaluated -> True];

you can use Plot[...] as usual.

An example:

 s = DSolve[y'[x] == 1/(1 + y[x]), y, x]; 
 Plot[ y[x] /. s /. C[1] -> Range[0, 5], {x, -5, 5}]

enter image description here

share|improve this answer
And why is Evaluate required? –  rcollyer Jun 28 '12 at 4:41
Plot[Evaluate@Normal[Series[Sin[x], {x, 0, 3}]], {x, -[Pi], [Pi]}] but if I try to do both as Plot[{Sin[x], Evaluate@Normal[Series[Sin[x], {x, 0, 3}]]}, {x, -[Pi], [Pi]}], it doesn't work while Plot[{Sin[x], x - x^3/6}, {x, -[Pi], [Pi]}] does. –  highBandWidth Jun 28 '12 at 4:43
Do I need to edit my answer to say different form which he did not originally show but has now edited his question to include? :^) –  Mr.Wizard Jun 28 '12 at 7:43
@rcollyer, good question ... but I am afraid because of my limited understanding Mma evaluation sequence I can only guess that it has to do with the fact Plot has HoldAll attribute :) Perhaps comments and answers on SE question may provide more concrete clues. –  kguler Jun 28 '12 at 8:30
@Mr.Wizard, I was trying to make sense of your comment for the last 15 minutes ... which seemed puzzling as your answer did not show on my screen despite several edits and screen refreshes that I made during the last several hours. I will delete the part in my post that uses the Evaluated option. –  kguler Jun 28 '12 at 8:51
show 1 more comment

Your Answer


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.