Two Words of Caution
2.3 Graphing Functions, Expressions, and EquationsEquations
2.3.1 Functions of a Single Variable
The command
Plot[f[x],{x,a,b}]
graphs the functiony fxon the intervala, b. Mathematica returns information about the basic syntax of thePlotcommand with?Plotor use theHelp Browser to obtain detailed information regardingPlot.
Remember that every Mathematica object can be assigned a name, including graph-ics.Show[p1,p2,...,pn]displays the graphicsp1,p2, ...,pntogether.
-2 2 4 6
-1 -0.5 0.5 1
Figure 2-1 ysinxforΠ x 2Π
EXAMPLE 2.3.1: GraphysinxforΠ x 2Π.
SOLUTION: Entering
In[107]:= p1 PlotSinx,x,Π, 2Π
graphsy sinxforΠ x 2Πand names the resultp1. The plot is shown in Figure 2-1.
EXAMPLE 2.3.2: Graphstfor0 t 5wherest 1for0 t < 1 andst 1st1fort1.
SOLUTION: After definingst,
In[108]:= st 1/0 t < 1 st 1st1/t1 we usePlotto graphstfor0 t 5in Figure 2-2.
In[109]:= Plotst,t, 0, 5, AspectRatioAutomatic Of course, Figure 2-2 is not completely precise: vertical lines are never the graphs of functions. In this case, discontinuities occur att 1,2,3,4, and 5. If we were to redraw the figure by hand, we would erase the
1 2 3 4 5 2
3 4 5
Figure 2-2 st 1st1,0 t 5
vertical line segments, and then for emphasis place open dots at1,1, 2,2,3,3,4,4, and5,5and then closed dots at 1,2,2,3,3,4, 4,5, and5,6.
Entering Options[Plot]lists all Plot options and their default values. The most frequently used options include PlotStyle, DisplayFunction, AspectRatio,PlotRange,PlotLabel, andAxesLabel.
1. PlotStyle controls the color and thickness of a plot. PlotStyle->
GrayLevel[w], where 0 w 1 instructs Mathematica to generate the plot in GrayLevel[w]. GrayLevel[0] corresponds to black and GrayLevel[1]corresponds to white. Color plots can be generated using RGBColor.RGBColor[1,0,0]corresponds to red,RGBColor[0,1,0]
corresponds to green, and RGBColor[0,0,1] corresponds to blue.
PlotStyle->Dashing[{a1,a2,...,an}] indicates that successive segments be dashed with repeating lengths of a1, a2,. . .,an. The thick-ness of the plot is controlled withPlotStyle->Thickness[w], where wis the fraction of the total width of the graphic. For a single plot, the PlotStyle options are combined with PlotStyle->{{option1, option2, ... , optionn}}.
2. A plot is not displayed when the option DisplayFunction->
Identity is included. Including the option DisplayFunction->$
DisplayFunctioninShoworPlotcommands instructs Mathematica to display graphics.
3. The ratio of height to width of a plot is controlled by AspectRatio.
The default is1/GoldenRatio. Generally, a plot is drawn to scale when the optionAspectRatio->Automaticis included in thePlotorShow command.
4. PlotRangecontrols the horizontal and vertical axes.PlotRange->{c,d}
specifies that the vertical axis displayed corresponds to the intervalc y dwhilePlotRange->{{a,b},{c,d}}specifes that the horizontal axis displayed corresponds to the intervala x band that the vertical axis displayed corresponds to the intervalc y d.
5. PlotLabel->"titleofplot"labels the plottitleofplot.
6. AxesLabel->{"xaxislabel","yaxislabel"}labels thex-axis with xaxislabeland they-axis withyaxislabel.
EXAMPLE 2.3.3: Graphysinx,ycosx, andytanxtogether with their inverse functions.
SOLUTION: Inp2andp3, we usePlotto graphysin1xandyx,
Be sure you have completed the previous example immediately before entering the following commands.
respectively. Neither plot is displayed because we include the option Display Function->Identity.p1, p2, andp3are displayed to-gether withShowin Figure 2-3. The plot is shown to scale; the graph of ysinxis in black,ysin1xis in gray, andyxis dashed.
-3 -2 -1 1 2 3
-3 -2 -1 1 2 3
Figure 2-3 ysinx,ysin1x, andyx
-3 -2 -1 1 2 3
-3 -2 -1 1 2 3
Figure 2-4 ycosx,ycos1x, andyx
In[110]:= p2 PlotArcSinx,x,1, 1, PlotStyleGrayLevel0.3, DisplayFunctionIdentity p3 Plotx,x,Π, 2Π,
PlotStyleDashing0.01, DisplayFunctionIdentity
p4 Showp1, p2, p3, PlotRange Π,Π,Π,Π, AspectRatioAutomatic
The commandPlot[{f1[x],f2[x],...,fn[x]},{x,a,b}]plots f1x, f2x,. . ., fnxtogether fora x b. SimplePlotStyleoptions are incorporated with PlotStyle->{option1,option2,..., optionn}where optionicorresponds to the plot of fix. Multiple options are incorporated using PlotStyle->{{options1}, {options2},...,{optionsn}} where optionsi are the options corresponding to the plot of fix.
In the following, we usePlot to graphy cosx,y cos1x, and y xtogether. Mathematica generates several error messages because the intervalΠ,Πcontains numbers not in the domain ofy cos1x.
Nevertheless, Mathematica displays the plot correctly in Figure 4-36.
-3 -2 -1 1 2 3
-3 -2 -1 1 2 3
Figure 2-5 ytanx,ytan1x, andyx
The plot is shown to scale; the graph ofycosxis in black,ycos1x is in gray, andyxis dashed.
In[111]:= r4 PlotCosx, ArcCosx, x,x,Π,Π, PlotStyle GrayLevel0, GrayLevel0.3, Dashing0.01,
PlotRange Π,Π, AspectRatioAutomatic Plot plnr arccosxis notamachinesize real number atx 3.14159.
Plot plnr arccosxis notamachinesize real number atx 2.8867.
Plot plnr arccosxis notamachinesize real number atx 2.60872.
General stop Further output of Plot plnr will be suppressed during this calculation.
We use the same idea to graph y tanx, y tan1x, and y x in Figure 2-5.
In[112]:= q4 PlotTanx, ArcTanx, x,x,Π,Π, PlotStyle GrayLevel0, GrayLevel0.3, Dashing0.01,
PlotRange Π,Π, AspectRatioAutomatic
-3 -2 -1 1 2 3
Figure 2-6 The elementary trigonometric functions and their inverses
UseShowtogether withGraphicsArrayto display graphics in rect-angular arrays. Entering
In[113]:= ShowGraphicsArrayp4, r4, q4
shows the three plotsp4,r4, andq4in a row as shown in Figure 2-6.
The previous example illustrates the graphical relationship between a function and its inverse.
EXAMPLE 2.3.4 (Inverse functions): fx and gxare inverse func-tionsif
fgx gfx x.
If fxandgxare inverse functions, their graphs are symmetric about the lineyx. The command
Composition[f1,f2,f3,...,fn,x]
computes the composition
f1 f2 fn x f1f2 fnx.
For two functions fxandgx, it is usually easiest to compute the com-position fgxwithf[g[x]]orf[x]//g.
Show that
fx 12x
4x and gx 4x1 x2 are inverse functions.
SOLUTION: After defining fxandgx,
fxandgxare not returned because a semi-colon is included at
the end of each command. In[114]:= fx 12 x
4x
gx 4 x1 x2
we compute and simplify the compositionsfgxandgfx. Because both results arex,fxandgxare inverse functions.
In[115]:= fgx Out[115]= 1221x4 x
4124 xx
In[116]:= Simplifyfgx Out[116]= x
In[117]:= Simplifygfx Out[117]= x
To see that the graphs offxandgxare symmetric about the lineyx, we usePlotto graph fx,gx, andyxtogether in Figure 2-7.
In[118]:= Plotfx, gx, fgx,x,10, 10, PlotStyle GrayLevel0, GrayLevel0.3, Dashing0.01, PlotRange 10, 10, AspectRatioAutomatic
In the plot, observe that the graphs offxandgxare symmetric about the liney x. The plot also illustrates that the domain and range of a function and its inverse are interchanged: fxhas domain,4 4,and range,2 2,;gxhas domain,2 2, and range,4 4,.
For repeated compositions of a function with itself,Nest[f,x,n]computes the composition
f f f f
ntimes
x fff
ntimes
x fnx.
EXAMPLE 2.3.5: Graph fx, f10x, f20x, f30x, f40x, and f50x if fx sinxfor0 x 2Π.
-10 -5 5 10
-10 -7.5 -5 -2.5 2.5 5 7.5 10
Figure 2-7 fxin black,gxin gray, andyxdashed
SOLUTION: After definingfx sinx, In[119]:= fx Sinx Out[119]= Sinx
we graph fxinp1withPlot
In[120]:= p1 Plotfx,x, 0, 2Π, DisplayFunctionIdentity and then illustrate the use ofNestby computing f5x.
In[121]:= Nestf, x, 5
Out[121]= SinSinSinSinSinx
Next, we useTabletogether withNestto create the list of functions f10x, f20x, f30x, f40x, f50x.
Because the resulting output is rather long, we include a semi-colon at the end of theTablecommand to suppress the resulting output.
In[122]:= toplot TableNestf, x, n,n, 10, 50, 10 Ingrays, we compute a list ofGrayLevel[i]for five equally spaced values ofibetween0.2and0.8. We then graph the functions intoplot
1 2 3 4 5 6
-1 -0.5 0.5 1
Figure 2-8 fxin black; the graphs off10x, f20x,f30x,f40x, andf50xare successively lighter – the graph of f50xis the lightest
on the interval0,2ΠwithPlot. The graphs are shaded according to graysand namedp2.Evaluate[toplot]causestoplotto be eval-uated before thePlotcommand. It is important: if you do not evaluate toplotfirst withEvaluate, Mathematica attempts to plottoplot.
Sincetoplotis not a function of a single variable, Mathematica gen-erates error messages and an empty plot. When Mathematica evaluates toplotfirst, Mathematica understands thattoplotis a list of func-tions and graphs each as expected.
Finally, we useShowtogether with the option
DisplayFunction->$DisplayFunction
to displayp1andp2together in Figure 2-8.
In[123]:= grays TableGrayLeveli,i, 0.2, 0.8, 0.6/4 p2 PlotEvaluatetoplot,x, 0, 2Π,
PlotStylegrays,
DisplayFunctionIdentity
Showp1, p2, DisplayFunction$DisplayFunction In the plot, we see that repeatedly composing sine with itself has a flat-tening effect onysinx.
The command
ListPlot[{{x1,y1},{x2,y2},...,{xn,yn}}]
plots the list of points x1, y1,x2, y2, . . . ,xn, yn. The size of the points in the resulting plot is controlled with the optionPlotStyle->PointSize[w], where wis the fraction of the total width of the graphic. For two-dimensional graphics, the default value is0.008.
Remark. The command
ListPlot[{y1,y2,..,yn}]
plots the list of points1, y1,2, y2, . . . ,n, yn.
EXAMPLE 2.3.6: Graphy
9x2 x24 .
SOLUTION: We usePlotto generate the basic graph ofyshown in Figure 4-38(a). Observe that Mathematica generates several error mes-sages, which is because we have instructed Mathematica to plot the function on an interval that contains numbers not in the domain of the
function. Mathematica’s error
messages do not always mean that you have made a mistake entering a command.
General stop Further output of Plot plnr will be suppressed during this calculation.
Observe that the domain ofyis3,2 2,2 2,3. A better graph ofyis obtained by plottingyfor3 x 3and shown in Figure 4-38(b).
We then use thePlotRangeoption to specify that the displayed horizontal axis corresponds to7 x 7and that the displayed vertical axis cor-responds to7 y 7. The graph is drawn to scale because we include the optionAspectRatio->Automatic. In this case, Mathematica does not generate any error messages. Mathematica uses a point-plotting scheme to generate graphs. Coincidentally, Mathematica happens to not samplex !2so does not generate any error messages.
In[125]:= p2 PlotSqrt9xˆ2/xˆ24,x,3, 3, PlotRange 7, 7,7, 7,
AspectRatioAutomatic
To see the endpoints in the plot, we useListPlotto plot the points 3,0and3,0. The points are slightly enlarged in Figure 4-38(c) be-cause we increase their size usingPointSize.
-3 -2 -1 1 2 3
-1 -0.5 0.5 1
-6 -4 -2 2 4 6 x
-6 -4 -2 2 4 6 y
-3 -2 -1 1 2 3
-10 -5 5 10
-6 -4 -2 2 4 6
-6 -4 -2 2 4 6
Figure 2-9 (from left to right) (a)–(d) The four plotsp1,p2,p3, andp4combined into a single graphic
In[126]:= p3 ListPlot3, 0,3, 0, PlotStylePointSize0.02
Finally, we useShowto displayp2andp3together in Figure 4-38(d), where we have labeled the axes using theAxesLabeloption.
In[127]:= p4 Showp2, p3, AxesLabel "x", "y"
The sequence of plots shown in Figure 4-38, which combinesp1,p2, p3, andp4into a single graphic, is generated usingShowtogether with GraphicsArray.
In[128]:= ShowGraphicsArrayp1, p2,p3, p4
When graphing functions involving odd roots, Mathematica’s results may be sur-prising to the beginner. The key is to load theRealOnlypackage located in the Miscellaneousfolder (or directory) first.
EXAMPLE 2.3.7: Graphyx1/3x22/3x14/3.
SOLUTION: Entering
General stop Further output of Plot plnr will be suppressed during this calculation.
not only produces many error messages but does not produce the graph we expect (see Figure 2-10(a)) because many of us consideryx1/3x 22/3x14/3to be a real-valued function with domain,. Gener-ally, Mathematica does return a real number when computing the odd
root of a negative number. For example,x3 1has three solutions Solveis discussed in more detail in the next section.
In[130]:= s1 Solvexˆ31 0
When computing an odd root of a negative number, Mathematica has many choices (as illustrated above) and chooses a root with positive imaginary part – the result is not a real number.
In[132]:= N1ˆ1/3 Out[132]= 0.50.866025
-1 -0.75 -0.5 -0.25 0.25 0.5
To obtain real values when computing odd roots of negative numbers, load theRealOnlypackage that is located in theMiscellaneousfolder or directory.
After theRealOnlypackage has been loaded, reentering thePlot com-mand produces the expected graph. See Figure 2-10(b).
In[133]:= << Miscellaneous‘RealOnly‘
In[134]:= p2 Plotxˆ1/3x2ˆ2/3x1ˆ4/3, x,2, 3
In[135]:= ShowGraphicsArrayp1, p2