TExcelTAD
Home › Calculus in Depth › New Functions from Old Functions

New Functions from Old Functions

Complicated functions are built from simple ones in three ways: by transforming a graph (shifting, stretching, reflecting), by combining functions with arithmetic, and by composing one function inside another. Knowing how a function was built tells you its graph and its domain, and composition in particular is what the chain rule will differentiate.

For a first look, see Graph Transformations and Composition and Inverses.

Transformations

Let c>0c > 0. Starting from the graph of y=f(x)y = f(x):

New function Effect on the graph
f(x)+cf(x) + c, f(x)−cf(x) - c shift up / down by cc
f(x−c)f(x - c), f(x+c)f(x + c) shift right / left by cc
c⋅f(x)c \cdot f(x) stretch vertically by cc (compress if c<1c < 1)
f(cx)f(cx) compress horizontally by cc (stretch if c<1c < 1)
−f(x)-f(x) reflect in the xx-axis
f(−x)f(-x) reflect in the yy-axis
∣f(x)∣\lvert f(x) \rvert reflect the parts below the xx-axis upward

The rule that trips people up is the horizontal one: f(x−3)f(x - 3) moves the graph right. The reason is that the new function reaches the old value f(a)f(a) when x−3=ax - 3 = a, that is at x=a+3x = a + 3. Changes inside the function act on xx and look "backwards"; changes outside act on yy and look as expected.

When several transformations are combined, apply them in the order that the expression is evaluated: inside first (horizontal), outside last (vertical). For y=2sin⁡(3x−π)+1y = 2\sin(3x - \pi) + 1, write it as 2sin⁡(3(x−π/3))+12\sin(3(x - \pi/3)) + 1: compress horizontally by 3, shift right π/3\pi/3, stretch vertically by 2, shift up 1.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-np.pi, 2 * np.pi, 600)
base = np.sin(x)
new = 2 * np.sin(3 * x - np.pi) + 1
plt.figure(figsize=(6, 4))
plt.plot(x, base, label="sin x")
plt.plot(x, new, label="2 sin(3x − π) + 1")
plt.axhline(1, color="grey", lw=0.5, ls="--")
plt.legend()
print("range of new:", round(float(new.min()), 3), "to", round(float(new.max()), 3))
print("period:", round(2 * np.pi / 3, 4), " a cycle starts at x =", round(np.pi / 3, 4))
range of new: -1.0 to 3.0
period: 2.0944  a cycle starts at x = 1.0472

A cycle starts where 3x−π=03x - \pi = 0, at x=π/3x = \pi/3: there the graph crosses its midline y=1y = 1 going up, just as sin⁡x\sin x crosses 0 going up at x=0x = 0.

Combinations

For functions ff and gg, the sum, difference, product and quotient are defined pointwise: (f+g)(x)=f(x)+g(x)(f + g)(x) = f(x) + g(x), and so on. The domain is where both are defined, D(f)∩D(g)D(f) \cap D(g), and for f/gf/g we also remove the points where g(x)=0g(x) = 0.

Composition

The composition f∘gf \circ g is defined by (f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x)): first apply gg, then ff. Its domain is the set of xx in the domain of gg for which g(x)g(x) lies in the domain of ff. Composition is not commutative: f∘gf \circ g and g∘fg \circ f are usually different.

python · try it yourself
import sympy as sp

x = sp.symbols('x', real=True)
f = sp.sqrt(x)          # domain x >= 0
g = 2 - x**2            # domain all x
fg = f.subs(x, g)       # f(g(x))
gf = g.subs(x, f)       # g(f(x))
print("f∘g =", fg)
print("g∘f =", gf)
print("domain of f∘g:", sp.solve_univariate_inequality(g >= 0, x, relational=False))
print("(f∘g)(1) =", fg.subs(x, 1), "  (g∘f)(1) =", gf.subs(x, 1))
f∘g = sqrt(2 - x**2)
g∘f = 2 - x
domain of f∘g: Interval(-sqrt(2), sqrt(2))
(f∘g)(1) = 1   (g∘f)(1) = 1

Notice the trap in g∘fg \circ f: SymPy simplifies (x)2(\sqrt{x})^2 to xx and prints 2−x2 - x, which looks defined everywhere. But g∘fg \circ f only accepts x≥0x \ge 0, because x\sqrt{x} must be computed first. Simplifying a formula can erase its domain, just as cancelling a factor erased a hole in the diagnostic review.

Decomposition

The reverse skill, recognising a given function as a composition, matters more in calculus than building one. To differentiate h(x)=sin⁡2(5x+1)h(x) = \sin^2(5x + 1) with the chain rule, you must see it as three layers:

k(x)=5x+1(innermost)g(u)=sin⁡uf(v)=v2(outermost)h=f∘g∘k\begin{aligned} k(x) &= 5x + 1 && \text{(innermost)} \\ g(u) &= \sin u \\ f(v) &= v^2 && \text{(outermost)} \\ h &= f \circ g \circ k \end{aligned}

Read the layers in the order a calculator would evaluate them: first 5x+15x + 1, then the sine, then the square. A decomposition is not unique (you could merge the inner two layers into sin⁡(5x+1)\sin(5x + 1)), but the most useful one has each layer be a single basic function from the catalogue.

python · try it yourself
import math
from functools import reduce

def compose(*fs):
    """compose(f, g, k)(x) == f(g(k(x))), applied right to left."""
    return reduce(lambda f, g: lambda x: f(g(x)), fs)

k = lambda x: 5 * x + 1
g = math.sin
f = lambda v: v ** 2
h = compose(f, g, k)
for x in [0, 0.3, 1]:
    print(x, round(h(x), 6), round(math.sin(5 * x + 1) ** 2, 6))
0 0.708073 0.708073
0.3 0.358169 0.358169
1 0.078073 0.078073

In software

Composition is the backbone of programming: function pipelines, middleware stacks, Unix pipes, map chains and, above all, neural networks, where each layer is a function and the network is their composition, network(x)=layern(…layer2(layer1(x))…)\text{network}(x) = \text{layer}_n(\ldots\text{layer}_2(\text{layer}_1(x))\ldots). Training such a network needs the derivative of a composition, which is exactly what backpropagation computes (Backpropagation Is the Chain Rule). Transformations appear wherever a signal or curve is re-timed or re-scaled: f(t−delay)f(t - \text{delay}), f(speed⋅t)f(\text{speed} \cdot t), gain⋅f(t)+offset\text{gain} \cdot f(t) + \text{offset} in audio, animation and UI code.

Exercises

Exercise 1: Read the transformations

Describe how to get the graph of y=−2(x+3)2+1y = -2(x + 3)^2 + 1 from y=x2y = x^2, and give the vertex and the direction it opens.

Answer

Shift left 3, stretch vertically by 2, reflect in the xx-axis, shift up 1. Vertex (−3,1)(-3, 1), opens downward.

Solution

Inside: x+3x + 3 shifts the graph 3 units left, moving the vertex from (0,0)(0, 0) to (−3,0)(-3, 0). Outside: the factor −2-2 stretches by 2 and flips, so it opens downward; +1+1 raises the vertex to (−3,1)(-3, 1).

Exercise 2: Domain of a quotient

For f(x)=x+2f(x) = \sqrt{x + 2} and g(x)=x2−1g(x) = x^2 - 1, find the domain of f/gf/g.

Answer

x≥−2x \ge -2 with x≠−1x \ne -1 and x≠1x \ne 1, that is [−2,−1)∪(−1,1)∪(1,∞)[-2, -1) \cup (-1, 1) \cup (1, \infty).

Solution

ff needs x+2≥0x + 2 \ge 0, so x≥−2x \ge -2. gg is defined everywhere but the quotient excludes g(x)=0g(x) = 0, that is x=±1x = \pm 1. Both ±1\pm 1 lie in [−2,∞)[-2, \infty), so both are removed.

Exercise 3: Decompose

Write h(x)=sin⁡(x2)+1h(x) = \sqrt{\sin(x^2) + 1} as f∘g∘kf \circ g \circ k with each layer a simple function.

Answer

k(x)=x2k(x) = x^2, g(u)=sin⁡u+1g(u) = \sin u + 1, f(v)=vf(v) = \sqrt{v}.

Solution

Evaluate as a calculator would: square xx first (kk), then take the sine and add 1 (gg), then the square root (ff). You could also split gg into sin⁡u\sin u and u+1u + 1, giving four layers.

Exercise 4: Evaluate a composition in code

For f(x)=x2−1f(x) = x^2 - 1 and g(x)=1/(x+1)g(x) = 1/(x + 1), compute (f∘g)(2)(f \circ g)(2) exactly by hand, then with the compose helper using Fraction.

Answer

(f∘g)(2)=−89(f \circ g)(2) = -\frac{8}{9}.

Solution

g(2)=13g(2) = \frac{1}{3}, then f(13)=19−1=−89f(\frac{1}{3}) = \frac{1}{9} - 1 = -\frac{8}{9}.

python · try it yourself
from fractions import Fraction as F
from functools import reduce

compose = lambda *fs: reduce(lambda f, g: lambda x: f(g(x)), fs)
f = lambda x: x**2 - 1
g = lambda x: 1 / (x + 1)
print(compose(f, g)(F(2)))
-8/9

Exercise 5: When do they commute?

For f(x)=x2f(x) = x^2 and g(x)=x+1g(x) = x + 1, find all xx with (f∘g)(x)=(g∘f)(x)(f \circ g)(x) = (g \circ f)(x).

Answer

Only x=0x = 0.

Solution
(f∘g)(x)=(x+1)2=x2+2x+1(g∘f)(x)=x2+1x2+2x+1=x2+1 ⇒ 2x=0 ⇒ x=0\begin{aligned} (f \circ g)(x) &= (x + 1)^2 = x^2 + 2x + 1 \\ (g \circ f)(x) &= x^2 + 1 \\ x^2 + 2x + 1 &= x^2 + 1 \ \Rightarrow\ 2x = 0 \ \Rightarrow\ x = 0 \end{aligned}

Test yourself

How is the graph of y=f(x−4)y = f(x - 4) related to the graph of y=f(x)y = f(x)?

  1. Shifted left 4
  2. Shifted right 4
  3. Shifted down 4
  4. Stretched by 4

For f(x)=xf(x) = \sqrt{x} and g(x)=x−5g(x) = x - 5, what is the domain of f∘gf \circ g?

  1. x≥0x \ge 0
  2. x≥5x \ge 5
  3. x≥−5x \ge -5
  4. all real xx

Which decomposition of h(x)=ecos⁡xh(x) = e^{\cos x} is correct?

  1. f(u)=euf(u) = e^u, g(x)=cos⁡xg(x) = \cos x, h=f∘gh = f \circ g
  2. f(u)=cos⁡uf(u) = \cos u, g(x)=exg(x) = e^x, h=f∘gh = f \circ g
  3. h=ex⋅cos⁡xh = e^x \cdot \cos x
  4. f(u)=euf(u) = e^u, g(x)=cos⁡xg(x) = \cos x, h=g∘fh = g \circ f