Four Ways to Represent a Function
A function can be given in words, as a table of values, as a graph or as a formula, and each form answers different questions. This lesson states the precise definition, shows how to move between the four representations, and covers piecewise functions, symmetry and monotonicity, which later chapters use constantly.
For a first look at domain and range, see Functions, Domain and Range.
The definition
A function from a set to a set is a rule that assigns to each element of exactly one element of .
- is the domain. When a formula is given without a domain, the convention is the largest set of real numbers for which the formula makes sense.
- The range is the set of values actually takes: . It can be smaller than .
- The graph of is the set of pairs .
"Exactly one" is the whole point. A curve in the plane is the graph of a function of if and only if every vertical line meets it at most once (the vertical line test). The circle fails it: the line meets it at and . It is the union of two function graphs, and .
The four representations
| Form | Example | Good for | Weak at |
|---|---|---|---|
| verbal | "the monthly bill is a $5 base fee plus $0.12 per GB above 20 GB" | modelling, requirements | computing |
| numeric | a table of sensor readings every 10 minutes | measured data | values between the samples |
| visual | a plotted temperature curve | trends, extremes, symmetry | exact values |
| algebraic | exact computation, calculus | messy real data |
Moving between them is a core skill. The verbal billing rule becomes a piecewise formula:
Code is just another algebraic representation:
def bill(gb):
"""Monthly bill in dollars for gb gigabytes of traffic."""
if gb < 0:
raise ValueError("domain is gb >= 0")
return 5 + 0.12 * max(0, gb - 20)
for g in [0, 12.5, 20, 21, 50, 200]:
print(f"{g:>6} GB -> ${bill(g):.2f}") 0 GB -> $5.00
12.5 GB -> $5.00
20 GB -> $5.00
21 GB -> $5.12
50 GB -> $8.60
200 GB -> $26.60
The raise makes the domain explicit, something a formula on paper only states in words. The range is .
Piecewise functions and absolute value
The absolute value is the most important piecewise function:
so always, and is the distance between and on the number line. Sums of absolute values are piecewise linear: to rewrite one, split the line at the points where each expression inside changes sign.
Data to function
A table defines a function only at its sample points. Between them we must choose a model. The simplest is linear interpolation, which draws straight segments between neighbouring points:
import numpy as np
t = np.array([0, 10, 20, 30, 40]) # minutes
temp = np.array([18.0, 21.5, 23.0, 23.4, 23.5]) # °C
for q in [5, 15, 37]:
print(f"T({q}) ≈ {float(np.interp(q, t, temp)):.2f} °C")
coeffs = np.polyfit(t, temp, 2)
print("quadratic fit:", [round(float(c), 5) for c in coeffs])
for q in [37, 60]:
print(f"t = {q}: interp {float(np.interp(q, t, temp)):.2f}, fit {float(np.polyval(coeffs, q)):.2f}")T(5) ≈ 19.75 °C
T(15) ≈ 22.25 °C
T(37) ≈ 23.47 °C
quadratic fit: [-0.00564, 0.35471, 18.17143]
t = 37: interp 23.47, fit 23.57
t = 60: interp 23.50, fit 19.14
Inside the data range the two models agree closely (about 0.1 °C apart at ). Outside it they part ways: asked about , np.interp simply repeats the last value, while the quadratic, which has its peak near , predicts the room cooling back down by four degrees. Neither is "right"; extrapolation depends entirely on the model you chose, and choosing it is a modelling decision, not a calculation. The next lesson's catalogue of functions is the menu to choose from.
Symmetry
- is even if for every in the domain. Its graph is symmetric about the -axis (, , ).
- is odd if . Its graph is symmetric about the origin (, , ).
Products follow sign rules: and are even, is odd. Symmetry saves real work later: the integral of an odd function over is 0.
import sympy as sp
x = sp.symbols('x')
def parity(f):
g = f.subs(x, -x)
if sp.simplify(g - f) == 0:
return "even"
if sp.simplify(g + f) == 0:
return "odd"
return "neither"
for f in [x**4 - 3*x**2, x**3 * sp.cos(x), x / (x**2 + 1), sp.exp(x), sp.Abs(x) + x**2]:
print(f, "->", parity(f))x**4 - 3*x**2 -> even
x**3*cos(x) -> odd
x/(x**2 + 1) -> odd
exp(x) -> neither
x**2 + Abs(x) -> even
Increasing and decreasing
is increasing on an interval if in implies , and decreasing if it implies . The definition compares outputs for every pair of inputs, which is hard to check directly; in chapter 4 the derivative gives a practical test (How Derivatives Affect the Shape of a Graph).
In software
Every pure function in a program is a mathematical function: one output per input. A lookup table (dict, array) is the numeric representation, np.interp turns it into a continuous function, and a pricing rule, a tax bracket or a game's damage falloff is a piecewise function. Domain checks are input validation. Functions that are not functions in the mathematical sense, because they read a clock or a random generator, are exactly the ones that make tests flaky.
Exercises
Exercise 1: Domain from a formula
Find the domain of .
Answer
Solution
The square root needs and the division needs , so we need .
Exercise 2: Remove the absolute values
Write as a piecewise function without absolute values, and give its range.
Answer
for , for , for . Range .
Solution
The inside expressions change sign at and .
The outer pieces grow without bound and the middle piece is the smallest value, so the range is . Geometrically, is the total distance from to and to 1, which is at least 2.
Exercise 3: A box as a function
An open box is made from a sheet by cutting a square of side from each corner and folding up the sides. Write the volume with its domain, then estimate the largest volume numerically on a fine grid.
Answer
on . The maximum is about at .
Solution
After folding, the base is by and the height is . All three must be positive, so .
import numpy as np
x = np.linspace(0, 6, 600001)
V = x * (20 - 2*x) * (12 - 2*x)
i = int(np.argmax(V))
print(f"x ≈ {float(x[i]):.3f}, V ≈ {float(V[i]):.1f}")x ≈ 2.427, V ≈ 262.7
Chapter 4 finds the exact maximiser with a derivative.
Exercise 4: Parity by the rules
Without computing, decide whether is even, odd or neither, then confirm with the parity function above.
Answer
Odd.
Solution
is odd and is even, so is odd. is odd and is even, so is odd. A sum of odd functions is odd: .
Test yourself
Which curve is NOT the graph of a function of ?
is even and is odd. What is ?
- Even
- Neither, always
- Odd
- It depends on and
What is the natural domain of ?
- and