Sub topic 12
Piecewise Functions and Their Domains
This chapter assumes you can evaluate a function such as f(x) = 2x + 3, write sets in interval notation, and find the domain of a single formula by excluding division by zero and negative numbers under an even root. Here you will apply those skills to functions defined by several formulas, each with its own condition.
What a piecewise function is
A piecewise function uses different rules on different parts of its domain. Each rule is a piece. Each piece comes with a condition that says which inputs it covers. To evaluate the function at an input, you find the one condition that the input satisfies and use that piece's rule.
Here is an example, written one piece per row.
| Rule | Condition |
|---|---|
| x + 3 | x < 0 |
| x² | 0 ≤ x ≤ 2 |
| 6 − x | x > 2 |
Call this function f. In a textbook, the three rows are joined by one large brace on the left, and f(x) = is written before it. In plain text, the same function looks like this:
f(x) = x + 3 if x < 0
x^2 if 0 <= x <= 2
6 - x if x > 2A piecewise definition is still an ordinary function. Every allowed input produces exactly one output. The definition is only a way of writing the rule, so the rule can change from one stretch of the number line to another.
Why use piecewise definitions
Some quantities really do change behavior at a threshold. A shipping fee might be flat up to a weight limit and grow per kilogram after that. Absolute value is also piecewise:
| Rule | Condition |
|---|---|
| x | x ≥ 0 |
| −x | x < 0 |
For x = −5, the condition x < 0 holds, so the rule −x gives 5.
Evaluating at a value
Evaluation takes two steps. First, test the input against the conditions to select a piece. Second, substitute the input into that piece only. The other pieces play no part.
For the function f above:
- f(−2): −2 < 0, so use x + 3. The result is 1.
- f(0): 0 satisfies 0 ≤ x ≤ 2, so use x². The result is 0.
- f(1.5): the middle piece applies, so 1.5² = 2.25.
- f(2): 2 satisfies 0 ≤ x ≤ 2, so use x². The result is 4. It does not satisfy x > 2, so the third rule is not used here.
- f(5): 5 > 2, so use 6 − x. The result is 1.
Code makes the selection step explicit. Each if or elif is one condition, and the return line is that condition's rule.
def f(x):
if x < 0:
return x + 3
elif x <= 2: # reached only when x >= 0
return x ** 2
else: # reached only when x > 2
return 6 - x
print(f(-2), f(0), f(1.5), f(2), f(5)) # 1 0 2.25 4 1The diagram shows the same decision process.
The boundary rule
At a boundary value, exactly one piece must own the point. Look at which condition has the equals sign.
In f, the boundary at x = 0 belongs to the middle piece, because it has 0 ≤ x. The first piece uses x < 0, which excludes 0. The boundary at x = 2 also belongs to the middle piece, because of x ≤ 2. The third piece uses x > 2.
This matters for the graph. At x = 0, the first piece would give 0 + 3 = 3 if it covered 0, but it does not. The graph therefore has an open circle at (0, 3), where the first piece ends, and a closed circle at (0, 0), where the middle piece starts. The function has a jump at x = 0. At x = 2, the middle piece reaches 4 with a closed circle, and the third piece approaches 6 − 2 = 4 with an open circle at the same height. The two pieces meet there, so the graph has no break at x = 2.
The domain of a piecewise function
The domain of a piecewise function is the union of the domains of its pieces. The domain of one piece is the set of inputs that satisfy both of these requirements:
- The input meets the piece's condition.
- The piece's rule is defined at that input (no division by zero, no negative number under an even root).
For f, every rule is a polynomial, so requirement 2 never fails. The three conditions are x < 0, 0 ≤ x ≤ 2, and x > 2. Together they cover every real number, so the domain is (−∞, ∞).
When a rule restricts its own piece
Consider this function:
| Rule | Condition |
|---|---|
| 1 / (x − 3) | x < 5 |
| √(x − 5) | x ≥ 5 |
Piece 1 has condition x < 5, which gives (−∞, 5). The rule needs x ≠ 3. Remove 3 to get (−∞, 3) ∪ (3, 5).
Piece 2 has condition x ≥ 5, which gives [5, ∞). The rule needs x − 5 ≥ 0, so x ≥ 5. Both requirements say the same thing, and the piece's domain is [5, ∞).
The union is (−∞, 3) ∪ (3, 5) ∪ [5, ∞), which simplifies to (−∞, 3) ∪ (3, ∞). In set notation, that is {x | x ≠ 3}.
Simplifying is valid because (3, 5) and [5, ∞) join at 5 with no gap. The domain is an interval union that you can merge when the pieces touch.
import math
def h(x):
if x < 5:
if x == 3:
raise ValueError("x = 3 is outside the domain of h")
return 1 / (x - 3)
return math.sqrt(x - 5)
print(h(4)) # 1.0
print(h(1)) # -0.5
print(h(9)) # 2.0The ValueError appears inside the first branch because the exclusion at 3 belongs to that piece only. A condition and a restriction are different things. The condition says where a piece applies. The restriction says where its formula works.
Gaps and overlaps
A piecewise definition can leave out part of the number line on purpose. Suppose a function uses x + 1 for x < 1 and x² for x > 3. Inputs from 1 to 3 satisfy neither condition, so they are not in the domain. The domain is (−∞, 1) ∪ (3, ∞). A gap in the conditions is a gap in the domain.
The opposite problem is an overlap. If one piece says x ≤ 2 and another says x ≥ 2, then x = 2 satisfies both. If the two rules give different outputs at 2, the input 2 has two outputs, so the definition is not a function. Check every shared boundary and make sure at most one condition includes it, or that the rules agree there.
The range of a piecewise function
Find the range piece by piece, then take the union. For each piece, ask what outputs its rule produces on its condition's interval.
For f:
- Piece 1, x + 3 for x < 0: outputs are less than 3, so (−∞, 3).
- Piece 2, x² for 0 ≤ x ≤ 2: outputs run from 0 to 4, so [0, 4].
- Piece 3, 6 − x for x > 2: outputs are less than 4, so (−∞, 4).
The union is (−∞, 4]. The largest output is 4, reached at x = 2. The piece-3 values just below 4 are already covered, and the values 3 and below are covered by the other pieces.
A graph confirms this. Read the heights of all the filled parts of the curve and ignore the open circles, unless another piece fills the same height.
Common errors
| Error | Why it fails | Fix |
|---|---|---|
| Substituting into every piece | Only the piece whose condition holds applies | Test the conditions first, then substitute once |
| Ignoring a rule's own restriction | A condition can include a value where the formula is undefined | Intersect the condition with the rule's natural domain |
| Taking the domain of the first piece only | The domain is the union of all pieces | Combine every piece's domain |
| Closed circles at both ends of a jump | One input would have two outputs | Give the point to the piece with the equals sign |
| Reading the range from the formulas without the conditions | A rule such as x² gives [0, ∞) alone, but only [0, 4] on `0 ≤ x ≤ 2` | Apply each rule only on its own interval |
Practice
Exercise
Let p(x) = 2x − 1 for x < 3, and p(x) = x² − 4 for x ≥ 3. Find p(−1), p(3), and p(4).
Show answer
p(−1): −1 < 3, so use 2x − 1, giving −3. p(3): 3 ≥ 3, so use x² − 4, giving 5. p(4): 4 ≥ 3, so use x² − 4, giving 12.
Exercise
Find the domain of q(x) = √(x + 2) for −2 ≤ x < 1, and q(x) = 1 / (x − 4) for x > 1. Write it in interval notation.
Show answer
Piece 1: the condition gives [−2, 1), and √(x + 2) needs x ≥ −2, so the piece's domain is [−2, 1). Piece 2: the condition gives (1, ∞), and the rule needs x ≠ 4, so the domain is (1, 4) ∪ (4, ∞). The value x = 1 is in neither condition, so it is excluded. The domain is [−2, 1) ∪ (1, 4) ∪ (4, ∞).
Exercise
Find the range of r(x) = x + 1 for x ≤ 0, and r(x) = x² + 2 for x > 0.
Show answer
Piece 1: x ≤ 0 gives outputs ≤ 1, so (−∞, 1]. Piece 2: for x > 0, x² > 0, so outputs are greater than 2, giving (2, ∞). The union is (−∞, 1] ∪ (2, ∞). The values in (1, 2] are never produced.