Sub topic 8
Piecewise Functions and Breakpoints
This chapter assumes you can solve linear and quadratic equations, plot points, and work with inequalities and absolute value. It builds on modulus functions and graph reading from earlier chapters.
What a piecewise function is
A piecewise function is a function defined by different rules on different parts of its domain. Each rule is a piece. The input values where the rule changes are called breakpoints.
def f(x):
if x < 1:
return x + 2
elif x <= 3:
return 3
else:
return 6 - x
for x in [-2, 0, 1, 2, 3, 4, 6]:
print(x, f(x))This function is written as:
- f(x) = x + 2 for x < 1
- f(x) = 3 for 1 ≤ x ≤ 3
- f(x) = 6 − x for x > 3
The breakpoints are x = 1 and x = 3. To evaluate f(a), first decide which condition a satisfies, then use only that rule. Many errors come from substituting into the wrong piece, so write the condition test before you calculate.
Why breakpoints matter
Inside one piece, the function is a simple expression such as a line or a parabola. The interesting behaviour happens at breakpoints: the slope changes, the graph may jump, and maxima and minima often sit there. Finding breakpoints first reduces a hard-looking problem to a few easy cases.
Modulus expressions as piecewise functions
Every absolute value is a hidden piecewise function:
- |x − a| = x − a when x ≥ a
- |x − a| = a − x when x < a
The breakpoint is x = a, where the expression inside becomes zero. For an expression like |x − 2| + |x − 5|, collect every point where an inside expression is zero. Those points split the number line into regions, and in each region you can remove the bars with known signs.
| Region | \ | x − 2\ | Column | \ | x − 5\ | Column | f(x) | Slope |
|---|---|---|---|---|---|---|---|---|
| x < 2 | 2 − x | 5 − x | 7 − 2x | −2 | ||||
| 2 ≤ x ≤ 5 | x − 2 | 5 − x | 3 | 0 | ||||
| x > 5 | x − 2 | x − 5 | 2x − 7 | +2 |
The graph descends, stays flat at 3 between 2 and 5, then rises. The minimum value is 3 and it occurs on the whole interval [2, 5].
A procedure that always works
The check in the last step matters. If you solve x − 2 + 5 − x = 7 inside the region x < 2, any answer must satisfy x < 2, or it does not belong to that piece.
Worked example: solving an equation
Solve |x − 1| + |x − 4| = 7.
- Breakpoints are 1 and 4.
- For x < 1, the expression is (1 − x) + (4 − x) = 5 − 2x. Setting 5 − 2x = 7 gives x = −1, which is less than 1, so it is valid.
- For 1 ≤ x ≤ 4, the expression is 3, which never equals 7. There is no solution here.
- For x > 4, the expression is 2x − 5. Setting 2x − 5 = 7 gives x = 6, which is greater than 4, so it is valid.
The solutions are x = −1 and x = 6.
Slope changes at breakpoints
You can sketch these graphs without redoing the algebra in each region, using a slope rule. For f(x) = |x − a₁| + |x − a₂| + … + |x − aₙ|:
- Far to the left, every term has slope −1, so the total slope is −n.
- Each time you pass a breakpoint, one term's slope changes from −1 to +1. The total slope rises by 2.
- Far to the right, the slope is +n.
With three breakpoints, the slopes run −3, −1, +1, +3. The minimum is where the slope changes from negative to non-negative. This is the median of the breakpoints.
def f(x):
return abs(x - 1) + abs(x - 4) + abs(x - 6)
for p in [1, 4, 6]:
print(p, f(p))The output is 8, 5 and 7. The smallest is at x = 4, the median of 1, 4 and 6. Because the graph is made of straight segments, the extremes of a modulus sum always occur at breakpoints, so you only need to evaluate there.
With an even number of breakpoints, the minimum is flat across the interval between the two middle breakpoints, as in the 2 and 5 example.
With coefficients, such as 3|x − 1| + |x − 5|, the slope changes by 2 times the coefficient at each breakpoint. The minimum then sits at the breakpoint with the larger weight, here x = 1. A larger coefficient pulls the minimum toward its own breakpoint.
Continuity and jumps
A piecewise graph is continuous at a breakpoint if the pieces meet there. Test this by evaluating both neighbouring rules at the breakpoint. In the first example, at x = 1, the rule x + 2 gives 3 and the middle rule gives 3, so the graph joins. At x = 3, the middle rule gives 3 and 6 − x gives 3, so it joins again.
Modulus functions are always continuous. Greatest-integer (floor) and ceiling functions are not. They are piecewise constant, with a jump at every integer. For floor(x), the rule is floor(x) = n on the interval n ≤ x < n + 1, so the graph is a staircase.
- The floor graph has a filled dot at the left end of each step and an open dot at the right end.
- The ceiling graph has the opposite arrangement: an open dot on the left and a filled dot on the right.
When a piecewise function has a jump, a maximum or minimum may be missed because the extreme value is never actually attained at an open dot. Always check whether the endpoint belongs to the piece.
Maxima and minima on a piecewise graph
For a function with several pieces, find the extreme value of each piece on its own interval, then compare. For a linear piece, the extremes are at the interval ends. For a quadratic piece, check the vertex if it lies inside the interval, plus both ends. For a constant piece, the value is the same everywhere.
For the first function, the left piece rises toward 3, the middle piece is 3, and the right piece falls from 3. The maximum is 3, and it is attained on all of [1, 3]. There is no minimum, because both outer pieces go to −∞.
Common errors
- Forgetting a breakpoint. If two bars contain different expressions, there are two breakpoints and three regions. A missed one gives a wrong graph.
- Not checking that a solution lies in its region. A root of the simplified expression that falls outside the region is rejected.
- Mixing up ≤ and <. At a breakpoint, exactly one piece must claim the point, or the function is not well defined.
- Assuming the minimum is where the formula is smallest in a single piece. The graph's overall extreme may be at another piece's endpoint.
- Treating floor and ceiling as continuous. A jump can skip the value you want, so an equation like floor(x) = 2.5 has no solution.
Exercise
Find the minimum value of f(x) = |x − 3| + |x + 1| + |x − 7|, and the x at which it occurs.
Show answer
The breakpoints are −1, 3 and 7, and the median is 3. Compute f(3) = 0 + 4 + 4 = 8. Check f(−1) = 4 + 0 + 8 = 12 and f(7) = 4 + 8 + 0 = 12. The minimum is 8, at x = 3.
Exercise
How many real solutions does |x| + |x − 2| = 4 have? Find them.
Show answer
The breakpoints are 0 and 2. For x < 0, the expression is 2 − 2x. Setting 2 − 2x = 4 gives x = −1, which is valid. For 0 ≤ x ≤ 2, the expression is 2, so there is no solution. For x > 2, the expression is 2x − 2. Setting 2x − 2 = 4 gives x = 3, which is valid. There are 2 solutions: x = −1 and x = 3.
Exercise
Let f(x) = |x − 1| − |x − 4|. Write it piecewise and state its range.
Show answer
For x < 1, f(x) = (1 − x) − (4 − x) = −3. For 1 ≤ x ≤ 4, f(x) = (x − 1) − (4 − x) = 2x − 5, which rises from −3 to 3. For x > 4, f(x) = (x − 1) − (x − 4) = 3. The range is [−3, 3].