Math Insight

Function composition examples

Function composition is the combination of two function to form a new function. One simply takes the output of the first function and uses it as the input to the second function. This process works as long as the second function will take the first function's output as its input (i.e., the second function's domain must contain the first function's range). Composition can be illustrated using the function machine metaphor by connecting function machines together.

Here, we demonstrate the process of composing functions with a series of examples.

Example 1: The grandmother function

The mother function $m$ is a function that takes a person $x$ as an input and output that person's mother $m(x)$. We can form the grandmother $g$ function by composing the mother function with itself. Applying this combined function to a persoon $x$ will output the mother of the mother of person $x$, which we could denote as $g(x) = m(m(x))$. The grandmother function is well-defined because the range of the mother function $m$ is the set of women who have had children, and this set is a subset of the mother function's domain, which is the set of all people. We won't run into trouble applying the mother function twice. (We'll ignore the problem of the origin of our species and not worry about folks such as Adam and Eve).

Example 2

Let $f$ be a function from the set of real numbers to the set of real numbers, which we write as $f: \R \to \R$. Similarly, let $g$ be another such function $g: \R \to \R$. Since we know $g$ will always output real numbers, there will be no trouble feeding the output of $g$ into $f$, since $f$ can take any real number as input. We can define a new function $h: \R \to \R$ by $h(x)=f(g(x))$ for any real number $x$.

To use specific formulas, let $f(x)=\frac{1}{1+x^2}$ and let $g(x)=x^2-x$. Then, the composition is $$h(x)=f(g(x)) = \frac{1}{1+(x^2-x)^2}.$$

Example 2'

If it is confusing to have the variable $x$ play two different roles in the above example, we could use different variable names. For example, let $f(y)=\frac{1}{1+y^2}$ and let $g(x)=x^2-x$. Then, to form the composition $h(x)=f(g(x))$, we can see that we replace the $y$ with $x^2-x$ and get the same answer $$h(x)=f(g(x)) = \frac{1}{1+(x^2-x)^2}.$$

Example 2''

Using yet different variable names for the same problem, let $f(\bigstar)=\frac{1}{1+\bigstar^2}$, let $g(\bigtriangleup)=\bigtriangleup^2-\bigtriangleup$, and let the $h$ be the composition $h(\bigtriangleup) = f(g(\bigtriangleup))$, which we can write as $$h(\bigtriangleup) = f(g(\bigtriangleup))=\frac{1}{1+(\bigtriangleup^2-\bigtriangleup)^2}.$$

All these variations of example 2 refer to the same function. We can write the composition by avoiding variable names as $h = f \circ g$. The only difficulty is to remember that we apply $g$ first even though it is at the right of the formula.

Example 3

Using the same functions $f(x)=\frac{1}{1+x^2}$ and $g(x)=x^2-x$, we can also compose them in the other direction to form the function $b = g \circ f$. The function $b$ is different from the function $h$ of the previous example, as the order in which we apply the functions makes a difference. The formula for $b$ is $$b(x) = (g \circ f)(x)=g(f(x))= \frac{1}{(1+x^2)^2} - \frac{1}{1+x^2}.$$ Here we replaced every $x$ in $g(x)$ with the expression for $f(x)$.

Example 4

It clearly doesn't make sense to compose the mother function $m$ from example 1 with the above function $f(x)=\frac{1}{1+x^2}$. The composition $f \circ m$ won't work because $m$ outputs people but $f$ expects a real number as the input. If you think of the function machine metaphor, the people coming out the $m$'s function machine will get stuck in the input funnel of $f$'s function machine. In the same way, the composition $m \circ f$ won't work, as the mother function $m$ won't know what to do with the numbers that $f$ outputs.

Example 5

Let $f$ be a function $f: \R_{\ge 0} \to \R$, where the domain $\R_{\ge 0}$ is the set of non-negative real numbers, $\R_{\ge 0} = \{x \in \R : x \ge 0\}$. Can we define the composition $f \circ g$ where $g$ is a function $g: \R \to \R$? The domain of $f$ is the non-negative reals but the codomain of $g$ is entire real line. Without knowing the range of $g$, we can't rule out the possibility that $g$ might output a negative number.

If $g(x)$ could be a negative number for some input $x$, then $f$ wouldn't know what to do with the input $g(x)$, and the composition $f \circ g$ wouldn't be defined. If, on the other hand, we knew the range of $g$ was contained in $\R_{\ge 0}$, then we could conclude that the composition $f \circ g$ can be safely done. In that case, no matter the input $x$ of $g$, we could know that $g(x)$ is non-negative and is a valid input for $f$.

For example, let $f: \R_{\ge 0} \to \R$ be defined by $f(x)=\sqrt{x}$. Let $g: \R \to \R$ be defined by $g(x)=x^3+1$. Given that the domain of $g$ is all real numbers, the range of $g$ also includes negative numbers. The composition $f(g(x))=\sqrt{x^3+1}$ isn't defined with this definition of $g$. To make this composition well-defined, we'd have to change the domain of $g$ (for example, exclude inputs less than -1) to make sure it never outputs a negative number.

On the other hand, if $g: \R \to \R$ were defined by $g(x)=x^2+1$, then we know its range is contained in $\R_{\ge 0}$. The composition $f \circ g$ can be safely evaluated with the formula $f(g(x))=\sqrt{x^2+1}$ for all real inputs $x$.