TExcelTAD
Home › Calculus Essentials › Functions, Domain and Range

Functions, Domain and Range

A function is a rule that takes an input and gives back exactly one output. Calculus is the study of how functions change, so before anything else you need to be fluent in what a function is, which inputs it accepts (its domain) and which outputs it can produce (its range).

The definition

A function ff from a set AA to a set BB assigns to each element xx of AA exactly one element f(x)f(x) of BB.

f(x)=x2+1“f of x equals x squared plus one”f(3)=32+1=10input 3, output 10\begin{aligned} f(x) &= x^2 + 1 && \text{``f of x equals x squared plus one''} \\ f(3) &= 3^2 + 1 = 10 && \text{input 3, output 10} \end{aligned}
  • xx is the input or independent variable.
  • f(x)f(x) (often written yy) is the output or dependent variable.
  • The domain is the set of allowed inputs.
  • The range is the set of outputs that actually occur.

The key word is exactly one. The rule "yy is a number whose square is xx" is not a function, because x=4x = 4 gives both y=2y = 2 and y=−2y = -2. On a graph, this is the vertical line test: a curve is the graph of a function only if no vertical line crosses it more than once. A circle fails the test; the upper half of a circle passes.

A Python function is the same idea in code, as long as it is pure (no randomness, no hidden state): the same argument always returns the same value.

python · try it yourself
def f(x):
    return x**2 + 1

for x in [-2, -1, 0, 1, 2, 3]:
    print(f"f({x:2}) = {f(x)}")
f(-2) = 5
f(-1) = 2
f( 0) = 1
f( 1) = 2
f( 2) = 5
f( 3) = 10

Different inputs may share an output (f(−2)=f(2)=5f(-2) = f(2) = 5); that is allowed. One input with two outputs is not.

Finding the domain

If no domain is stated, the domain is every real number for which the formula makes sense. In real-number calculus three things go wrong:

Problem Rule Example Domain
Division by zero denominator≠0\text{denominator} \ne 0 1x−3\frac{1}{x - 3} all xx except 3
Even root of a negative radicand≥0\text{radicand} \ge 0 x+2\sqrt{x + 2} x≥−2x \ge -2
Log of a non-positive number argument>0\text{argument} > 0 ln⁡(5−x)\ln(5 - x) x<5x < 5

When several restrictions apply, all must hold at once. For g(x)=xx−4g(x) = \frac{\sqrt{x}}{x - 4} you need x≥0x \ge 0 and x≠4x \ne 4, so the domain is [0,4)∪(4,∞)[0, 4) \cup (4, \infty).

Interval notation is the usual shorthand: square brackets include an endpoint, round brackets exclude it, and ∞\infty always gets a round bracket.

Inequality Interval
−1≤x≤3-1 \le x \le 3 [−1,3][-1, 3]
0<x≤50 < x \le 5 (0,5](0, 5]
x≥2x \ge 2 [2,∞)[2, \infty)
all reals (−∞,∞)(-\infty, \infty)

SymPy can find domains and ranges for you:

python · try it yourself
import sympy as sp
from sympy.calculus.util import continuous_domain, function_range

x = sp.symbols('x', real=True)
R = sp.S.Reals

print(continuous_domain(1 / (x - 3), x, R))
print(continuous_domain(sp.sqrt(x + 2), x, R))
print(continuous_domain(sp.log(5 - x), x, R))
print(continuous_domain(sp.sqrt(x) / (x - 4), x, R))
print(function_range(x**2 + 1, x, R))
Union(Interval.open(-oo, 3), Interval.open(3, oo))
Interval(-2, oo)
Interval.open(-oo, 5)
Union(Interval.Ropen(0, 4), Interval.open(4, oo))
Interval(1, oo)

Interval.Ropen(0, 4) means "open on the right", that is [0,4)[0, 4).

Finding the range

The range is usually harder than the domain. Two approaches:

  1. Reason about the formula. x2x^2 is never negative, so x2+1≥1x^2 + 1 \ge 1, and every value from 1 upward is reached. The range of x2+1x^2 + 1 is [1,∞)[1, \infty).
  2. Solve for xx. For y=1x−3y = \frac{1}{x - 3}: x=3+1yx = 3 + \frac{1}{y}. That works for every yy except 0, so the range is all reals except 0.

A plot is a good sanity check:

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

xs = np.linspace(-2, 10, 400)
ys = np.sqrt(xs + 2)

plt.figure(figsize=(6, 4))
plt.plot(xs, ys)
plt.scatter([-2], [0], color="C3", zorder=3, label="starts at (−2, 0)")
plt.xlabel("x"); plt.ylabel("√(x + 2)")
plt.grid(alpha=0.3); plt.legend()
plt.title("Domain [−2, ∞), range [0, ∞)")

print("smallest output:", ys.min(), " output at x = 10:", round(ys[-1], 4))
smallest output: 0.0  output at x = 10: 3.4641

Ways to describe a function

Form Example Good for
Formula A(r)=πr2A(r) = \pi r^2 exact work, calculus
Table hourly temperature readings measured data
Graph a stock chart seeing the shape
Code def area(r): return math.pi * r * r computing
Piecewise ∣x∣=x if x≥0, −x if x<0\lvert x \rvert = x \text{ if } x \ge 0,\ -x \text{ if } x < 0 rules that change by region

In software

  • Pure functions in programming match the mathematical definition, which is why they are easy to test, cache (memoise) and run in parallel.
  • Domain errors are real bugs. math.sqrt(-1) raises ValueError: math domain error and math.log(0) raises too; NumPy returns nan or -inf with a warning. Validating inputs is checking the domain.
  • Type signatures such as def f(x: float) -> float describe the domain and codomain, though not the exact range.
  • Lookup tables in games and embedded code store a function as a table of precomputed outputs.

Exercises

Exercise 1: Evaluate

For h(x)=2x2−3xh(x) = 2x^2 - 3x, find h(0)h(0), h(−1)h(-1) and h(a+1)h(a + 1).

Answer

h(0)=0h(0) = 0, h(−1)=5h(-1) = 5, h(a+1)=2a2+a−1h(a + 1) = 2a^2 + a - 1.

Solution
h(0)=0−0=0h(−1)=2⋅1+3=5h(a+1)=2(a2+2a+1)−3a−3=2a2+4a+2−3a−3=2a2+a−1\begin{aligned} h(0) &= 0 - 0 = 0 \\ h(-1) &= 2 \cdot 1 + 3 = 5 \\ h(a + 1) &= 2(a^2 + 2a + 1) - 3a - 3 = 2a^2 + 4a + 2 - 3a - 3 = 2a^2 + a - 1 \end{aligned}

Exercise 2: Domains by hand

Give the domain of (a) f(x)=1x2−9f(x) = \frac{1}{x^2 - 9}, (b) g(x)=6−2xg(x) = \sqrt{6 - 2x}, (c) k(x)=ln⁡(x)x−1k(x) = \frac{\ln(x)}{x - 1}.

Answer

(a) all reals except −3 and 3; (b) (−∞,3](-\infty, 3]; (c) (0,1)∪(1,∞)(0, 1) \cup (1, \infty).

Solution
  • (a) x2−9=0x^2 - 9 = 0 at x=±3x = \pm 3, so exclude both.
  • (b) 6−2x≥06 - 2x \ge 0 gives x≤3x \le 3.
  • (c) ln⁡\ln needs x>0x > 0 and the denominator needs x≠1x \ne 1.

Exercise 3: Range by solving for xx

Find the range of y=2x+1x−1y = \frac{2x + 1}{x - 1}.

Answer

All reals except 2.

Solution
y(x−1)=2x+1yx−y=2x+1x(y−2)=y+1x=y+1y−2works for every y except y=2\begin{aligned} y(x - 1) &= 2x + 1 \\ yx - y &= 2x + 1 \\ x(y - 2) &= y + 1 \\ x &= \frac{y + 1}{y - 2} && \text{works for every } y \text{ except } y = 2 \end{aligned}
python · try it yourself
import sympy as sp
from sympy.calculus.util import function_range

x = sp.symbols('x', real=True)
f = (2*x + 1) / (x - 1)
print(function_range(f, x, sp.Interval.open(1, sp.oo)))
print(function_range(f, x, sp.Interval.open(-sp.oo, 1)))
Interval.open(2, oo)
Interval.open(-oo, 2)

Together the two branches cover everything except 2.

Exercise 4: A safe square root

Write a Python function safe_sqrt(x) that returns None outside the domain of x\sqrt{x} and the root otherwise. Test it on −4, 0 and 2.25.

Answer

None, 0.0, 1.5.

Solution
python · try it yourself
import math

def safe_sqrt(x):
    if x < 0:
        return None          # outside the domain [0, ∞)
    return math.sqrt(x)

for v in [-4, 0, 2.25]:
    print(v, safe_sqrt(v))
-4 None
0 0.0
2.25 1.5

Test yourself

What is the domain of f(x)=x−1x−5f(x) = \frac{\sqrt{x - 1}}{x - 5}?

  1. x≥1x \ge 1
  2. x≠5x \ne 5
  3. [1,5)∪(5,∞)[1, 5) \cup (5, \infty)
  4. (1,5)(1, 5)

Which of these is NOT a function of xx?

  1. y=x3y = x^3
  2. y=∣x∣y = \lvert x \rvert
  3. y2=xy^2 = x
  4. y=7y = 7

What is the range of f(x)=3−x2f(x) = 3 - x^2?

  1. (−∞,3](-\infty, 3]
  2. [3,∞)[3, \infty)
  3. all reals
  4. [0,3][0, 3]