SCCC Mathematica Tutorial, © 2007-2020, Seattle Central Community College Math Dept., contact: Greg.Langkamp@seattlecolleges.edu
Version 1.9/ March 2020
Important! ​
​Click "Make your own Copy" in the black ribbon above to make this document interactive. You will also need to be logged into Wolfram Cloud

Lesson 5 Graphing

5.1 How to generate a simple graph

Clear[f,g,t,x]
The basic syntax for the Plot command is:
Plot[expression, {x, xmin, xmax}] .
This will produce a graph of expression as a function from
x=xmin
to
x=xmax
. Notice that the second argument,
{x,xmin,xmax}
, which we will refer to as the "plot domain", requires the use of curly braces { } .
◼
  • Plot the graph of the function
    y=3
    2
    x
    -8
    for
    x
    between -5 and 5. Note that we have entered just the formula for the function, i.e.
    3
    2
    x
    -8
    , in the Plot command below.
  • Plot[3x^2-8,{x,-5,5}]
    Note for Mathematica Online users: In the plot above, you may be seeing black lines near the numbers along the x- and y-axes. This glitch is being addressed by Wolfram at this time.
    On the other hand, if you have already defined a function, say,
    f(x)=3
    2
    x
    -8
    , then you can refer to the function directly in the Plot command.
    f[x_]:=3x^2-8
    Plot[f[x],{x,-5,5}]
    There is no need to restrict ourselves to the variable
    x
    . In the next example we use the variable
    t
    throughout.
    Plot[Sin[t]+Sin[8t],{t,0,10}]
    ​
     Caution: If you use a variable other than
    x
    in your Plot command, be sure that you use it, and not
    x
    , in the plot domain. If you use
    t
    for the expression, but continue to use
    x
    for the plot domain, you will get an empty plot (see below). This is a common error made by new users.
    Plot[Sin[t]+Sin[8t],{x,0,10}]
    Exercise 5.1 A
    Plot the function
    y=sin(x)
    over two complete periods.
    ​
    Answer to Exercise 5.1A
    Plot[Sin[x],{x,0,4π}]

    5.2 The PlotRange and Exclusions options

    Let's start by plotting the function
    y=5cos(x)+15
    for
    -2π≤x≤2π
    .
    Plot[5Cos[x]+15,{x,-2π,2π}]
    In the above plot, Mathematica's graph appears to touch the x-axis, but look closely -- the axes are actually crossing at the point (0, 10). Why is this so? Mathematica automatically chooses a vertical scale so that the graph "fills out" the plot window.
    In the examples in the previous section we set the plot domain, but we let Mathematica automatically decide the plot range. As you can see with this example, there may be times when you want to manually control the vertical scale. To do this you use the PlotRange option described below.
    The PlotRange option.
    The PlotRange option allows you to specify the range of output values displayed on the vertical axis. We will consider five possible values for the PlotRange variable.
    PlotRangeAll
    . All computed output values are plotted. ​​
    PlotRangeAutomatic
    . This will exclude extreme values. If you do not specify a particular value for the PlotRange, then Mathematica assumes that you want it set to Automatic (which is the default). ​​
    PlotRange{ymin,ymax}
    . The vertical scale is displayed only from
    y=ymin
    to
    y=ymax
    . ​​
    PlotRangenumber
    . The vertical scale is displayed only from
    y=-number
    to
    y=number
    . ​​
    PlotRange{{xmin,xmax},{ymin,ymax}}
    . The horizontal scale is displayed from
    x=xmin
    to
    x=xmax
    and the vertical scale from
    y=ymin
    to
    y=ymax
    .
    Let's again plot the function
    y=5cos(x)+15
    for
    -2π≤x≤2π
    . This time we'll use the PlotRange command to set the vertical scale from 0 to 20.
    Plot[5Cos[x]+15,{x,-2π,2π},PlotRange{0,20}]
    Now we examine the All and Automatic values for PlotRange. Generally speaking these values produce the same graph. However, if there are extreme values generated, for example, near a vertical asymptote, then the graphs are very different. Compare the next two graphs below.
    ◼
  • Plot the graph of the function
    y=
    2
    x
    -1
    x-2
    for
    x
    between -2 and 4. Set the PlotRange option equal to All.
  • Plot[(x^2-1)/(x-2),{x,-2,4},PlotRange->All]
    Note that in order to accommodate the large range of y values produced by this function near
    x=2
    , the vertical scale is quite large. As a result, all of the details of the local behavior for this function are lost in the picture. For example we cannot see that the graph crosses the x axis at
    x=1
    and
    x=-1
    .
    ◼
  • Plot the graph of the function
    y=
    2
    x
    -1
    x-2
    for
    x
    between -2 and 4. Set the PlotRange option equal to Automatic.
  • Plot[(x^2-1)/(x-2),{x,-2,4},PlotRange->Automatic]
    Note that this time the graph does not extend nearly as far in the vertical direction. We can still see the vertical asymptote, however we now also clearly see the
    x-intercepts
    at
    x=1
    and
    x=-1
    .
    ◼
  • Plot the graph of the function
    y=
    2
    x
    -1
    x-2
    for
    x
    between -2 and 4. Set the PlotRange option equal to
    {-2,10}
    .
  • Plot[(x^2-1)/(x-2),{x,-2,4},PlotRange->{-2,10}]
    ◼
  • Plot the graph of the function
    y=
    2
    x
    -1
    x-2
    for
    x
    between -2 and 4. Set the PlotRange option equal to 15 to get a y scale from -15 to 15.
  • Plot[(x^2-1)/(x-2),{x,-2,4},PlotRange->15]
    ​
    The Exclusions option
    Note: This option is not needed for Mathematica Online, or with the latest versions of Desktop Mathematica.
    When we graphed the rational function
    y=
    2
    x
    -1
    x-2
    above, you might see a vertical looking line near
    x=2
    . You may have noticed a similar result when using a graphing calculator. The graph is distorted near
    x=2
    because it is not defined at
    x=2
    . The Exclusions option gives you a way of telling Mathematica where there are points of discontinuity so that it can produce a more accurate graph near those points. The Exclusions option allows us to remove vertical lines at asymptotes when they appear.
    ◼
  • Plot the graph of the function
    y=
    2
    x
    -1
    x-2
    for
    x
    between -2 and 4. Set the Exclusions option equal to 2 to handle the vertical asymptote at
    x=2
    .
  • Plot[(x^2-1)/(x-2),{x,-2,4},Exclusions2]
    Here is another example from trigonometry.
    ◼
  • Plot the graph of the function
    y=tan(x)
    for
    x
    between
    -π
    and
    π
    . Exclude the points
    -π
    2
    and
    π
    2
    from the plot domain.
  • Plot[Tan[x],{x,-π,π},Exclusions->{-π/2,π/2}]
    For sake of comparison here is the same function graphed without the Exclusions option.
    Plot[Tan[x],{x,-π,π}]
    As a final example, let's graph the sine function over the interval -π/2 ≤ x ≤ π/2 . Use the PlotRange command to set the x scale from -2π to 2π, and the y scale from -2 to 2.
    Plot[Sin[x],{x,-π/2,π/2},PlotRange{{-2π,2π},{-2,2}}]
    Exercise 5.2 A
    Plot the function
    y=sin(x)+7
    over the domain
    -5≤x≤5
    . Set the vertical scale to the interval
    -2≤y≤10
    .
    ​
    Answer to Exercise 5.2A
    Plot[Sin[x]+7,{x,-5,5},PlotRange{-2,10}]
    Exercise 5.2 B
    Plot the function
    y=
    2
    x-3
    on the interval
    -1≤x≤5
    .
    ​
    Answer to Exercise 5.2B
    Exercise 5.3 A
    Answer to Exercise 5.3A
    Ticks
    GridLines
    PlotLabel
    AxesLabel
    Exercise 5.4 A
    Answer to Exercise 5.4A
    Exercise 5.4 B
    Answer to Exercise 5.4B
    Exercise 5.4 C
    Answer to Exercise 5.4C
    ​
    ​
    Exercise 5.5 A
    Answer to Exercise 5.5A
    Exercise 5.5 B
    Answer to Exercise 5.5B
    Exercise 5.6 A
    Answer to Exercise 5.6A
    Exercise 5.7 A
    Answer to Exercise 5.7A
    Exercise 5.7 B
    Answer to Exercise 5.7B