# Mathematica plotting several functions

• Mathematica
Hi

I need to plot $\sin(x)$ and its first,third, fifth power series expansion on the same
plot. here's what I did

Code:
T[n_,x_]:=Normal[Series[Sin[x], {x, 0, n}]]

Plot[{Sin[x], T[1, x], T[3, x], T[5, x]}, {x, -2, 2}]

But I got following errors.

Code:
General::ivar: -1.99992 is not a valid variable. >>
General::ivar: -1.99992 is not a valid variable. >>
General::ivar: -1.99992 is not a valid variable. >>
General::stop: Further output of General::ivar will be suppressed during this calculation. >>

And in the end it plotted only sin(x). so whats happening ?

## Answers and Replies

T[n_, x_] := Normal[Series[Sin[x], {x, 0, n}]]
Plot[Evaluate[{Sin[x], T[1, x], T[3, x], T[5, x]}], {x, -2, 2}]

phyzguy
Science Advisor
I don't quite understand why this type of thing doesn't work, but if you make the following change it will work:

Code:
T[n_,x_]:=Normal[Series[Sin[z], {z, 0, n}]]/.z->x

Plot[{Sin[x], T[1, x], T[3, x], T[5, x]}, {x, -2, 2}]

thanks, both the solutions indeed work. But as phyzguy wondered, I don't quite understand
why mine would not work....

Evaluation of expressions in Mathematica is much more complicated than it appears.

Expressions in a Mathematica notebook look similar to expressions in BASIC or FORTRAN or c or python. So you might think they would be evaluated in a similar way. Mathematica actually goes will out of its way to make it appear that is what is happening. But under the surface the process is actually completely different from what you might think is happening.

BASIC and FORTRAN are moving bytes from one location to another, sometimes doing a calculation in the process. You can think this is pretty mechanical.

Mathematica only does that as an almost irrelevant detail. ALL of Mathematica is looking at a vast list of rules "if you see this pattern then you replace it with that pattern" and it keeps doing that until things stop changing. And then there are hundreds of special cases and exceptions that change the order patterns are replaced with patterns. Some of those are controlled by Attributes.

Perhaps half the problems people have with Mathematica are because they don't see just under the surface of how Mathematica is doing evaluation. And there are much much much deeper levels than that.

Plot[] has Attributes HoldAll and some others that aren't important. That says that Plot does not evaluate the things inside the [] in the usual way and the evaluation is pushed down to another level. The := you used in your definition of T[n_,x_] is really just an alias for SetDelayed[] and SetDelayed has Attributes HoldAll, again pushing evaluation down to another level.

I have not chased the problem all the way to the bottom to determine exactly why, if possible, that your original isn't working.

You can see some superficial information about what is going on and might be able to use this to diagnose where the problem is showing up if you compare the results of these two versions:

Trace[T[n_,x_]:=Normal[Series[Sin[x],{x,0,n}]];
Plot[Evaluate[{Sin[x],T[1,x],T[3,x],T[5,x]}],{x,-2,2}]]

Trace[T[n_,x_]:=Normal[Series[Sin[x],{x,0,n}]];
Plot[{Sin[x],T[1,x],T[3,x],T[5,x]},{x,-2,2}]]

But learning how to interpret the output of those is an art in itself.

It looks like, and this is from just glancing at the errors for a few seconds, that you need the Series to be working with the symbol x first and only later do you want Plot to be working with the various approximate real number values substituted for x. I'm only guessing that the way it was originally written the numeric values were getting into the game early at the level of Series and that was blowing up Series.

Evaluate has a very high priority and changes the usual evaluation process and forced early evaluation of the Series before (mostly) anything else happened. That allowed the Series to be finished before Plot started messing with the decimals.

Like almost everything to do with the depths of evaluation, some or maybe many of the things I've written above about evaluation are not completely precisely exactly true, but as I remember reading somewhere long ago, "that doesn't mean it isn't good for you."

Years ago I thought it might be possible to write an excellent example Mathematica evaluator in Mathematica and do this in a very clear understandable way. I remember decades ago that was done for Scheme and I learned a lot by spending a couple of days understanding how that worked. But nobody I've ever pressed to do that has said it would even be possible, let alone educational.

Thanks Bill. good explanation. "Evaluate" seems little tricky....