# 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: A function is a mapping from an input space to an output space.

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.

In everyday speech, the word “graphic” refers to any drawing. In mathematics, a “graph” is a drawing made in the Cartesian plane, with the input values marked by the horizontal axis and the function output marked by the vertical axis. Figure 4.2: The mapping from Figure 4.1 translated into the form of a graph. The input space is marked by the horizontal axis and the output space by the vertical axis. Each of the arrows in Figure 4.1 is represented by a point, whose x-coordinate is the position of the tail of the arrow in the input space and whose y-coordinate is the position of the head of the arrow in the output space. This is 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.

Math in the world

Nerve cells communicate via electrical voltages and currents. For long-distance communications (distances longer than about 1 mm) the signaling takes the form of pulses of voltage occurring repetitively at different rates. The formation of these pulses, called “action potentials,” was the subject of an extensive research project in the 1950s involving inserting tiny electrodes into relatively large (“giant”) nerve cells that mediate the fleeing reaction of squid. A typical experiment involved regulating artificially the voltage across the cell membrane. Through these experiments, the scientists— John Eccles, Alan Hodgkin, and Andrew Huxley—were able to describe mathematically the relationship between membrane voltage and the current across the membrane. A calculus model built from the relationships provided a concise description of the biophysics of action potentials. A 1963 Nobel Prize was awarded for this work. Figure 4.3: Data (simulated) from the squid giant axon experiments. A smooth curve is drawn through the data points.

In each experimental run, the membrane voltage was fixed at a given level (say, -50 mV) and the current measured. Figure 4.3 shows what the data might have looked like, each point showing the results of one experimental run.

The data points themselves might be described metaphorically as “clouds” spotting the voltage vs current “sky.” Real-world clouds often show patterns such as animal or country shapes. We might say that a given cloud resembles a rabbit. Similarly, the data clouds show a pattern between current and voltage. We might, for instance, describe the current-voltage relationship as S-shaped. Or, rather than using the letter “S” we could draw a curve through the dots to summarize and simplify the relationship.

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.

1. 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.
2. 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)) Figure 4.4: Graph of the function $$f(t) \equiv t e^t$$.

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.

The graphics frame in Figure 4.4 is a 2-dimensional area for drawing. The function graphed in the frame, $$f(t) \equiv y e^y$$, has a domain consisting of the entire number line, that is, the space of all real numbers. The plot, however, shows only a finite interval $$0 \leq t \leq 10$$ of that domain.

### 4.1.2 Contour plot Figure 4.5: Phase diagram for CO_2_. Source Ben Finney, Mark Jacobs, CC0, via Wikimedia Commons
Example Solid CO_2

There is considerable interest in ways to remove CO_2 from the atmosphere and store it permanently underground. It is hard to store gasses in the massive quantities needed to mitigate climate change. But CO_2_ storage is part of a system that includes temperature, pressure, and chemical affinity.

Figure 4.5 shows the relationship between the physical form of pure CO_2_ and the temperature and pressure of the gas. One way to present the relationship in functional form is to treat temperature and pressure as two inputs. The output of the function is one of several discrete values: liquid, gas, solid, supercritical fluid. The overall picture is that the domain of the function, the two-dimensional space defined by the two quantities pressure and temperature, is broken into non-overlapping regions.

The functions we will work with have a continuous output. To draw a contour plot of output, we plot the output by splitting it into many consecutive intervals, say, 0 to 10, 10 to 20, 20 to 30, and so on. This effectively divides the domain into zones, for instance, one zone covering all of the points in the input space where the output is between 10 and 20, another zone where the output is 20 to 30, etc. The boundaries between zones are called the contour lines.

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)) Figure 4.6: Contour plot of a function with two inputs $$g(y,z) \equiv e^{-z}\sin(y)$$

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.

The word “argmax” is a contraction of “argument of the maximum.” We will tend to use the word “input” instead of “argument”, but it means the same thing to say “the inputs to a function” as saying “the arguments of a function.”

For this contour graph

1. Find an input coordinate where the function is steepest.
2. 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)$$.

Why call the graphs of one-input functions slice plots rather than simply graphs?

Saying “graph” for a display of $$f(x)$$ versus $$x$$ is correct and reasonable. But in MOSAIC Calculus we have another point to make.

Almost always, when mathematically modeling a real-world situation or phenomenon, we do not try to capture every nuance of every relationship that might exist in the real world. We leave some things out. Such simplifications make modeling problems easier to deal with and encourage us to identify the most important features of the most important relationships.

In this spirit, it is useful always to assume that our models are leaving something out and that a more complete model involves a function with more inputs than the present candidate. The present candidate model should be considered as a slice of a more complete model. Our slice leaves out one or more of the input quantities in a more complete model.

As you become practiced reading contour plots, you might prefer to read this one as a hilltop (shaded yellow) side-by-side with a hollow or bowl (shaded purple), with green, almost level flanks at the left and right edges of the frame.

To illustrate this, suppose that the actual system involves relationships among three quantities, which we represent in the form of a function of two inputs, as shown in Figure 4.8. (The third quantity in the relationship is the output of the function.)

The most common forms of slice involve constructing a simpler function that has one input but not the other. For example, our simpler function might ignore input 22. There are different ways of collapsing the function of two inputs into a function of one input. An especially useful way in calculus is to take the two-input function and set one of the inputs to a constant value.

For instance, suppose we set input 22 to the constant value 1.5. This means that we can consider any value of input 1, but input 2 has been replaced by 1.5. In Figure 4.9, we’ve marked in red the points in the contour plot that give the output of the simplified function. Figure 4.9: Left: A slice (red) through the domain of a contour plot. Right: The value of the function along the red slice presented as a mathematical graph, generated by slice_plot().

Each point along the red line corresponds to a specific value of input #1. From the contours, we can read the output corresponding to each of those values of input #1. This relationship, output versus input #1 can be drawn as a mathematical graph (to the right of the contour plot). Study that graph until you can see how the rising and falling parts of the graph correspond to the contours being crossed by the red line.

Slices can be taken in any direction or even along a curved path! The blue line in Figure 4.10 shows the slice constructed by letting input 2 vary and holding input 1 at the constant value 0. Figure 4.10: A path (blue) along which to cut a slice. The graph is made with contour_plot(). Right: Slice plot along the blue path. The graph is made with slice_plot().

## 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?

1. slice_plot(x^2 ~ x, x=c(-3,3))
2. plot(x^2 ~ x, domain=c(-3,3))
3. slice_plot(x^2, bounds(x=c(-3,3)))
4. None of them correspond to the plot.
5. slice_plot(x^2 ~ x, bounds(x=c(-3,3)))

Drill 9 Which command made the plot in Figure 4.15?

1. None of them correspond to the plot.
2. slice_plot(pnorm(x) ~ x, bounds(x=(-4, 4))
3. slice_plot(pnorm(x) ~ x, bounds(x=c(-4, 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?

1. slice_plot(dnorm(y)) ~ x, bounds(y=c(-3,3)))
2. slice_plot(pnorm(x) ~ x, bounds(y=c(-4, 4)))
3. slice_plot(exp(y) ~ y, bounds(y=c(-4, 4)))
4. slice_plot(log(x) ~ x, bounds(x=c(0; 10)))

## 4.4 Exercises

#### Exercise 4.01

1. Fill in the table with the values of the function shown in the graph.
$$x$$ $$f(x)$$
-3
-2
-1
0
1
2
3
1. 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.

1. Find three points on the surface where $$f(x, y)=15$$. (It does not have to be exactly 15, just close.)

2. Find a point where $$f(x=2, y) = 12$$.

3. 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?

1. Sides $$a$$ and $$b$$ are reversed.
2. $$c$$ can never be more than $$b$$.
3. $$c$$ can never be more than $$a+b$$.
4. $$c$$ can never be more than $$a+b$$ or less than $$b-a$$.
5. $$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$$?

1. slice_plot(sin(x), bounds(x=-4:5))
2. slice_plot(sin(z) ~ x, bounds(x=-4:5))
3. slice_plot(sin(z), bounds(z=-4:5))
4. 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:

1. Find a value for the (x, y) inputs where the function output is about -0.35.

2. Find a value for the (x, y) inputs where the function output is about 0.9.

3. 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?

4. 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.

1. How many input variables are there?

2. What’s the function output when air temperature is 90 F and humidity is 50%?

3. True or false: Holding humidity constant, as the air temperature rises, the heat index also increases.

4. True or false: Holding air temperature constant, as humidity rises the heat index also rises.

5. True or false: Heat index increases with temperature at a different rate when the humidity is low compared to when the humidity is high.

6. True or false: Heat index increases with temperature most rapidly when the humidity is low.

7. 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?”