Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica plotting several functions

  1. Jun 27, 2012 #1
    Hi

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

    Code (Text):

    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 (Text):
    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 ?
     
  2. jcsd
  3. Jun 27, 2012 #2
    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}]
     
  4. Jun 27, 2012 #3

    phyzguy

    User Avatar
    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 (Text):

    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}]
     
     
  5. Jun 27, 2012 #4
    thanks, both the solutions indeed work. But as phyzguy wondered, I don't quite understand
    why mine would not work....
     
  6. Jun 27, 2012 #5
    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.
     
  7. Jun 28, 2012 #6
    Thanks Bill. good explanation. "Evaluate" seems little tricky....
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Mathematica plotting several functions
Loading...