SCCC Mathematica Tutorial, © 2007-2020, Seattle Central Community College Math Dept., contact: Greg.Langkamp@seattlecolleges.edu
Version 1.9/ March 2020
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
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
Lesson 5 Graphing
5.1 How to generate a simple graph
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 to . Notice that the second argument, , which we will refer to as the "plot domain", requires the use of curly braces { } .
x=xmin
x=xmax
{x,xmin,xmax}
◼
Plot the graph of the function for between -5 and 5. Note that we have entered just the formula for the function, i.e. , in the Plot command below.
y=3-8
2
x
x
3-8
2
x
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, , then you can refer to the function directly in the Plot command.
f(x)=3-8
2
x
f[x_]:=3x^2-8
Plot[f[x],{x,-5,5}]
There is no need to restrict ourselves to the variable . In the next example we use the variable throughout.
x
t
Plot[Sin[t]+Sin[8t],{t,0,10}]
Caution: If you use a variable other than in your Plot command, be sure that you use it, and not , in the plot domain. If you use for the expression, but continue to use for the plot domain, you will get an empty plot (see below). This is a common error made by new users.
x
x
t
x
Plot[Sin[t]+Sin[8t],{x,0,10}]
Exercise 5.1 A
Plot the function over two complete periods.
y=sin(x)
Answer to Exercise 5.1A
Plot[Sin[x],{x,0,4π}]
5.2 The PlotRange and Exclusions options
5.2 The PlotRange and Exclusions options
Let's start by plotting the function for .
y=5cos(x)+15
-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.
PlotRangeAll
PlotRangeAutomatic
PlotRange{ymin,ymax}
y=ymin
y=ymax
PlotRangenumber
y=-number
y=number
PlotRange{{xmin,xmax},{ymin,ymax}}
x=xmin
x=xmax
y=ymin
y=ymax
Let's again plot the function for . This time we'll use the PlotRange command to set the vertical scale from 0 to 20.
y=5cos(x)+15
-2π≤x≤2π
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 for between -2 and 4. Set the PlotRange option equal to All.
y=-1
2
x
x-2
x
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 , 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 and .
x=2
x=1
x=-1
◼
Plot the graph of the function for between -2 and 4. Set the PlotRange option equal to Automatic.
y=-1
2
x
x-2
x
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 at and .
x-intercepts
x=1
x=-1
◼
Plot the graph of the function for between -2 and 4. Set the PlotRange option equal to .
y=-1
2
x
x-2
x
{-2,10}
Plot[(x^2-1)/(x-2),{x,-2,4},PlotRange->{-2,10}]
◼
Plot the graph of the function for between -2 and 4. Set the PlotRange option equal to 15 to get a y scale from -15 to 15.
y=-1
2
x
x-2
x
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 above, you might see a vertical looking line near . You may have noticed a similar result when using a graphing calculator. The graph is distorted near because it is not defined at . 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.
y=-1
2
x
x-2
x=2
x=2
x=2
◼
Plot the graph of the function for between -2 and 4. Set the Exclusions option equal to 2 to handle the vertical asymptote at .
y=-1
2
x
x-2
x
x=2
Plot[(x^2-1)/(x-2),{x,-2,4},Exclusions2]
Here is another example from trigonometry.
◼
Plot the graph of the function for between and . Exclude the points and from the plot domain.
y=tan(x)
x
-π
π
-π
2
π
2
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 over the domain . Set the vertical scale to the interval .
y=sin(x)+7
-5≤x≤5
-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 on the interval .
y=
2
x-3
-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