4 Visualizing functions
In Chapter Chapter 1 we encouraged you to think of a function in terms of the space of possible inputs—the domain—and another space of outputs. In this chapter, we introduce ways to visualize the input and output space of a function together.
Let’s start with a space suitable for representing a single quantity. That is, of course, the familiar number line.
The number line is just the horizontal line. The vertical tick marks, like , are merely aids for human interpretation.
On the number line, any specific quantity is a single point. For instance, let’s use the number line to show the two quantities \(\color{blue}{6.3}\) and \(\color{magenta}{-4.5}\).
A function connects each point in the input space to a corresponding point in the output space. Figure Figure 4.1 gives a sketch of an arbitrary function where the input and output spaces are shown one above the other. Arrows are drawn for several input values, showing where those values are mapped to in the output space. (There are infinitely more arrows to be drawn in between those shown, but the graphic would become illegible.)
Figure 4.1 gives information about the function, but it is in a format that is almost impossible to interpret. Renė Descartes (1596–1650) proposed a better visualization: place the input and output spaces at right angles to one another and, instead of the head and tail of the arrows connecting corresponding positions on the input and on the output spaces, use the head and tail values as coordinates in the joint input/output space. This setup is shown in Figure 4.2 and is called, as you know, the graph of the function.
A major advantage of this format is that the function output can be displayed not just at a discrete set of input values, but at every point in the continuous input space. Figure 4.2 shows one function consistent with the discrete points, but there are many such functions. That is, the discrete arrows shown in Figure 4.1 do not completely specify a unique function.
The smooth curve in Figure 4.3 describes the relationship between current and voltage quantitatively. For example, if you know that the current is 0, you can use the curve to figure out what the voltage will be around -90 mV or -50 mV or -10 mV. But when the current is 0, the voltage will not be, say, -75 or -150.
Graphs such as Figure 4.5 and Figure 4.3 are good ways of showing relationships. We can even do calculations simply using such graphs. Place your finger on a point of the S-shaped graph and you can read from the axes an allowed pair of voltage and current values. Place your finger on a point on the vertical axis. Moving it to the curve will reveal what current is associated with that voltage.
Functions are, like graphs, a way of representing relationships. For all their advantages as a means of communication, graphs have their limits. With a graph, it is feasible only to show the relationship between two quantities or among three quantities. Functions can involve more quantities. For instance, the triangle-area function \[A(a,b,c) \equiv \frac{1}{4}\sqrt{\strut 4a^2b^2-(a^2+b^2-c^2)^2}\] gives the relationship between four quantities: the area and the lengths of the triangle’s three sides.
On the other hand, functions cannot represent all types of relationships. For instance, the curve in Figure 4.3 shows a relationship between current and voltage in nerve cells. But there is no mathematical function voltage(current) that does justice to the relationship. The reason is that mathematical functions can have one and only one output for any given input. There are three reasonable values for membrane voltage that are experimentally consistent with a zero current, not just one.
Care needs to be taken in using functions to represent relationships. For the nerve-cell current-voltage relationship, for instance, we can construct a function current(voltage) to represent the relationship. That is because for any given value of voltage there is just one corresponding current. But there is no voltage(current) function, even though knowing the current tells you a lot about the voltage.
4.1 Function graphs in R/mosaic
Given a function, it is easy to draw the corresponding relationship as a graphic. This section describes how to do that for functions that have one or two inputs. Another common task—given a relationship, represent it using functions—is usually not so easy and will require modeling techniques that we will develop in Block 1.
Contemporary practice is to draw graphs of functions using a computer. R/mosaic provides several functions that do this, you need only learn how to use them.
There are two essential arguments shared by all of the R/mosaic functions drawing a graph:
A tilde. Make sure you can find it on your keyboard.
- The function that is to be graphed. This is to be written as an R tilde expression. Some examples of tilde expressions are shown in the table below. The essential feature of a tilde expression is the mark, called a “tilde,” which serves as punctuation dividing a left side of the expression from a right side.
- The bounds. The domain of many functions reaches infinity, but our computer screens are not so big! Making a graph requires choosing a finite interval for each of the input quantities.
The tilde expression for a function with one input will have only one name on the right-hand side of the . The domain interval specification must use the same name:
Tilde expression | Domain interval specification |
---|---|
x^2 ~ x |
bounds(x = -3:3) |
y * exp(y) ~ y |
bounds(y = 0:10) |
log(y) / exp(y) ~ y |
bounds(y = -5:5) |
sin(z) / z ~ z |
bounds(y = -3*pi:3*pi) |
4.1.1 Slice plot
To draw a graph of a function with one input, use slice_plot()
. The tilde expression is the first argument; the domain interval specification is the second argument. For instance,
slice_plot(t * exp(t) ~ t, bounds(t=0:10))
Recall the situation seen in Figure 4.3 which shows a two-dimensional space of all possible (voltage, current) pairs for nerve cells. The experimental data identified many possible pairs—marked by the dots in Figure 4.3 —that are consistent with the relationships of the nerve-cell system.
The same is true of Figure 4.4, the graph of a function with a single input. The two-dimensional space shown in the Figure 4.4 contains (input, output) pairs, only a small fraction of which are consistent with the relationship described by the function. The points in that small fraction could be marked by individual dots, but instead of dots, we draw a continuous curve connecting the dots. Every point on the curve is consistent with the relationship between input and output represented by the function.
4.1.2 Contour plot
Functions with two inputs can be displayed with contour_plot()
. Naturally, the tilde expression defining the function will have two names on the right-hand side of ~. Similarly, the domain specification will have two arguments, one for each of the names in the tilde expression.
contour_plot(exp(-z)*sin(y) ~ y & z, bounds(y=-6:6, z=0:2))
Contour plots will be a preferred format for displaying functions with two inputs. The main reason to prefer contour plots is the ease with which locations of points in the input space can be identified and the ability to read output values without much difficulty.
4.1.3 Surface plot
There is another way to think about graphing functions with two inputs. There are in such a situation three quantities involved in the relationship. Two of these are the inputs, the third is the output. A three-dimensional space consists of all the possible coordinate triples; the relationship between the inputs and the output is marked by ruling out almost all of the potential triples and marking those points in the space that are consistent with the function.
the space of all possibilities (y, z, output) is three-dimensional, but very few of those possibilities are consistent with the function to be graphed. You can imagine our putting dots at all of those consistent-with-the-function points, or our drawing lots and lots of continuous curves through those dots, but the cloud of dots forms a surface; a continuous cloud of points floating over the (y, z) input space.
Figure 4.7 displays this surface. Since the image is drawn on a two-dimensional screen, we have to use painters’ techniques of perspective and shading. In the interactive version of the plot, you can move the viewpoint for the image which gives many people a more solid understanding of the surface.
surface_plot(exp(-z)*sin(y) ~ y & z, bounds(y=-6:6, z=0:2))
Note that the surface plot is made with the R/mosaic surface_plot()
, which takes arguments in the same way as contour_plot()
.
4.2 Interpreting contour plots
It may take some practice to learn to read contour plots fluently but it is a skill that is worthwhile to have. Note that the graphics frame is the Cartesian space of the two inputs. The output is presented as contour lines. Each contour line has a label giving the numerical value of the function output. Each of the input value pairs on a given contour line corresponds to an output at the level labeling that contour line. To find the output for an input pair that is not on a contour line, you interpolate between the contours on either side of that point.
What’s the value of the function being plotted here at input \((\text{input}_1=0, \text{input}_2=0)\)?
The input pair (0, 0)—which is marked by a small red dot—falls between the contours labeled “20” and “22.” This means that the output corresponding to input (0, 0) is somewhere between 20 and 22. The point is much closer to the contour labeled “20”, so it is reasonable to see the output value as 20.5. This is, of course, an approximation, but that is the nature of reading numbers off of graphs.
Often, the specific numerical value at a point is not of primary interest. Instead, we may be interested in how steep the function is at a point, which is indicated by the spacing between contours. When contours are closely spaced, the hillside is steep. Where contours are far apart, the hillside is not steep, perhaps even flat.
Another common task for interpreting contour plots is to locate the input pair that is at a local high point or low point: the top of a hill or the bottom of a hollow. Such points are called local argmax or local argmin respectively. The output of the function at a local argmax is called the local maximum; similarly for a local argmin, where the output is called a local minimum.
For this contour graph
- Find an input coordinate where the function is steepest.
- Find input coordinates for the high and low points of the function .
A function is steepest where contour lines are spaced closely together, that is, where the function output changes a lot with a small change in input. This is true near inputs \((x=0, y=1)\). But notice that steepness involves a direction. Near \((x=0,y=1)\), changing the \(x\) value does not lead to a big change in output, but a small change in the value of \(y\) leads to a big change in output. In other words, the function is steep in the y-direction but not in the x-direction.
The highest output value explicitly marked in the graph is 8. We can imagine from the shapes of the contours surrounding the 8 contour that the function reaches a peak somewhere in the middle of the region enclosed by the 8 contour, near the input coordinate \((x=0, y=-1.5)\).
Similarly, the lowest output value marked is -10. In the middle of the area enclosed by the -10 contour is a local low point. That there are two such regions, one centered near input coordinate \((x=-0.5, y=3.3)\), the other at \((x=1.5, y=3.1)\).
4.3 Drill
Drill 1 Considering the function shown in Figure 4.11, at which of these inputs is the function output nearly zero?
\((x=0, y=1)\) \((x=1, y=5)\) \((x=0, y=6)\) \((x=-2, y=6)\)
Drill 2 Considering the function shown in Figure 4.11, at which of these inputs is the function output nearly 1?
\((x=0, y=1)\) \((x=1, y=5)\) \((x=0, y=6)\) \((x=-2, y=6)\)
Drill 3 Which of these is a positive-going zero crossing of \(g(t)\) where \[g(t) \equiv \sin\left(\frac{2\pi}{5}t-3\right)\ ?\]
\(t=15/2\pi\) \(t = 2 \pi/15\) \(t = 3\) None of the above
Drill 4 Which of the following is a positive-going zero crossing of \(g(t)\)? \[\sin\left(\frac{2 \pi}{5} (t - 3) \right)\]
\(t=-5\) \(t=-3\) \(t=0\) \(t=3\) \(t=5\)
Drill 5 Which input is a negative-going zero-crossing of the function graphed in Figure 4.12?
\(t = -2.5\) \(t = -1.25\) \(t = 0\) \(t = 1.25\) \(t = 2.5\)
slice_plot(sin(2*pi*t/5) ~ t, bounds(t=c(-5.2,5.2))) %>%
gf_refine(scale_x_continuous(breaks = -5:5))
Drill 6 Which input is a positive-going zero-crossing of the function graphed in Figure 4.13?
\(t = -5\) \(t = -2.5\) \(t = -1.25\) \(t = 1.25\) \(t = 2.5\)
Drill 7 Considering the function graphed in ?fig-rev2-05, at which of these inputs is the function output nearly \(-1\)?
\((x=0, y=1)\) \((x=1, y=5)\) \((x=0, y=6)\) \((x=-2, y=6)\)
Drill 8 Which command made this plot in Figure 4.14?
slice_plot(x^2 ~ x, x=c(-3,3))
plot(x^2 ~ x, domain=c(-3,3))
slice_plot(x^2, bounds(x=c(-3,3)))
- None of them correspond to the plot.
slice_plot(x^2 ~ x, bounds(x=c(-3,3)))
Drill 9 Which command made the plot in Figure 4.15?
- None of them correspond to the plot.
slice_plot(pnorm(x) ~ x, bounds(x=(-4, 4))
slice_plot(pnorm(x) ~ x, bounds(x=c(-4, 4)))
slice_plot(pnorm(x) ~ y, bounds(x=c(-4, 4)))
Drill 10 Only one of the following commands will successfully generate the graph of a function. Which one?
slice_plot(dnorm(y)) ~ x, bounds(y=c(-3,3)))
slice_plot(pnorm(x) ~ x, bounds(y=c(-4, 4)))
slice_plot(exp(y) ~ y, bounds(y=c(-4, 4)))
slice_plot(log(x) ~ x, bounds(x=c(0; 10)))
4.4 Exercises
Exercise 4.01
- Fill in the table with the values of the function shown in the graph.
\(x\) | \(f(x)\) |
---|---|
-3 | |
-2 | |
-1 | |
0 | |
1 | |
2 | |
3 |
- Fill in the table with the \(x\) values corresponding to the function values.
\(x\) | \(f(x)\) |
---|---|
0 | |
5 | |
12.5 | |
20 |
Exercise 4.02
The interactive figure displays a function, but we haven’t shown you any formula for the function, just the graph.
As you place the cursor on a point on the surface, a box will display the \((x,y,z)\) coordinates are displayed.
Find three points on the surface where \(f(x, y)=15\). (It does not have to be exactly 15, just close.)
Find a point where \(f(x=2, y) = 12\).
Explain why you can find multiple input points that generate an output of 15, but only one point where \(f(x=2, y)=12)\).
Exercise 4.03
A triangle consists of three connected line segments: the sides. It has other properties that are related to the sides and each other, for example, the angles between sides, the perimeter, or the area enclosed by the triangle.
Here’s a description of the relationship between the perimeter \(p\) and the lengths of the sides, \(a\), \(b\), \(c\), written in the form of a function:
\[p(a,b,c) \equiv a + b + c\ .\]
The mathematical expression of the relationship between area \(A\) enclosed by the triangle and the side lengths goes back at least 2000 years to Heron of Alexandria (circa 10–70). As a function, it can be written
\[A(a,b,c) \equiv \frac{1}{4}\sqrt{\strut 4a^2b^2-(a^2+b^2-c^2)^2}\ .\]
We cannot readily plot out this function because there are three inputs and one output, and it is impossible to draw in a 4-dimensional space . But we can draw a slice through the 4-dimensional space.
Let’s do that by setting, say, \(a=3\) and looking at the area as a function of the lengths of sides \(b\) and \(c\).
Part A Not all of the graphics frame is shaded. These are values of \(b\) and \(c\) for which the formula involves a negative quantity in the square root. Thinking about the nature of triangles, why the negative in the square root?
- Sides \(a\) and \(b\) are reversed.
- \(c\) can never be more than \(b\).
- \(c\) can never be more than \(a+b\).
- \(c\) can never be more than \(a+b\) or less than \(b-a\).
- \(c\) can never be more than \(a+b\) nor less than \(b-a\), nor less than \(a-b\).
Another slice might set \(a = b\), in which case we would be showing the areas of isosceles triangles.
Part B Draw a contour plot of the area of the triangle when \(a=b\). Referring to your graph, what’s the area of the isosceles triangle with \(a=b=4\)?
5 6 7 8 9
Exercise 4.04
Part A Which of the following R expressions creates a graph of the function \(\sin(z)\) with a graphics domain from \(-4\leq z \leq 5\)?
slice_plot(sin(x), bounds(x=-4:5))
slice_plot(sin(z) ~ x, bounds(x=-4:5))
slice_plot(sin(z), bounds(z=-4:5))
slice_plot(sin(z) ~ z, bounds(z=-4:5))
Hint #1: You can try copying and running each expression in an R console to check your answer.
Exercise 4.05
The contour plot below is marked with several colored lines which represent slices through the surface. Your job is to match these up with the slice plots presented below.
In the slice plots, the input \(t\) reflects the position on the slice. At \(t=0\), position is at the leftmost point of the slice, while at \(t=1\) position is at the right terminus of the slice.
Part A Which color line corresponds to slice 1?
black/solid gray/long-dashed blue/dotted tan/short-dashed yellow/dot-dashed
Part B Which color line corresponds to slice 2?
black/solid gray/long-dashed blue/dotted tan/short-dashed yellow/dot-dashed
Part C Which color line corresponds to slice 3?
black/solid gray/long-dashed blue/dotted tan/short-dashed yellow/dot-dashed
Part D Which color line corresponds to slice 4?
black/solid gray/long-dashed blue/dotted tan/short-dashed yellow/dot-dashed
Exercise 4.06
Consider this interactive contour plot:
Find a value for the (x, y) inputs where the function output is about -0.35.
Find a value for the (x, y) inputs where the function output is about 0.9.
One contour goes through the input A=(x=1.53, y=1.59). The neighboring contour goes through B=(x=1.53, y=0.90). What is the function output half-way between these two points, at input C=(x=1.53, y=1.25)? How does the output at C relate to the output at A and B?
Choose one of the contours and track the output as you move the cursor along that contour. What pattern do you see in the output?
Exercise 4.07
The US National Weather Service provides this graphic for calculating the “heat index.” Source link The output is the heat index, shown both quantitatively (the labels on the contours) and with color.
How many input variables are there?
What’s the function output when air temperature is 90 F and humidity is 50%?
True or false: Holding humidity constant, as the air temperature rises, the heat index also increases.
True or false: Holding air temperature constant, as humidity rises the heat index also rises.
True or false: Heat index increases with temperature at a different rate when the humidity is low compared to when the humidity is high.
True or false: Heat index increases with temperature most rapidly when the humidity is low.
Consider this statement: “When the air temperature is 102 F and the relative humidity is 77%, the wet-bulb temperature is about 95 F.” Is “heat index” the same as “wet-bulb temperature?”