Mathematica plotting several functions

In summary, the two versions of the plot do not seem to be working the same way. The original does not appear to be working because the Series needs to be working with the symbol x first before Plot can start working with the various approximate real number values substituted for x. The revised version does work because the Series is already working with the symbol x and Plot only starts working with the various approximate real number values substituted for x after the Series has been evaluated.
  • #1
issacnewton
1,000
29
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:
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 what's happening ?
 
Physics news on Phys.org
  • #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}]
 
  • #3
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}]
 
  • #4
thanks, both the solutions indeed work. But as phyzguy wondered, I don't quite understand
why mine would not work...
 
  • #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.
 
  • #6
Thanks Bill. good explanation. "Evaluate" seems little tricky...
 

1. How do I plot multiple functions on the same graph in Mathematica?

To plot multiple functions on the same graph in Mathematica, you can use the Plot or ParametricPlot functions. Simply input the list of functions you want to plot within the function, separated by commas. For example, Plot[{Sin[x], Cos[x], Tan[x]}, {x, 0, 2Pi}] will plot the sine, cosine, and tangent functions on the same graph.

2. Can I change the colors and styles of the plotted functions?

Yes, you can customize the colors and styles of the plotted functions in Mathematica. To change the color, you can use the PlotStyle option within the Plot or ParametricPlot function and specify the desired color. You can also use the Directive function to specify different styles such as dashed or thick lines.

3. How can I add labels and legends to my plot?

To add labels and legends to your plot in Mathematica, you can use the PlotLegends option within the Plot or ParametricPlot function. You can specify the labels for each function and customize the location and appearance of the legend. You can also use the Epilog option to add text or graphics directly onto the plot.

4. Is it possible to plot functions with different domains on the same graph?

Yes, you can plot functions with different domains on the same graph in Mathematica. You can use the RegionFunction option within the Plot or ParametricPlot function to specify the domain for each function. You can also use the Show function to combine multiple plots with different domains into one graph.

5. How do I change the axis labels and scale on my plot?

To change the axis labels and scale on your plot in Mathematica, you can use the AxesLabel and PlotRange options within the Plot or ParametricPlot function. You can specify the labels for each axis and customize the minimum and maximum values for the plot. You can also use the ScalingFunctions option to change the scale of the axes.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
259
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top