Rules that change partway through
Piecewise definitions, absolute value as a piecewise rule, and step functions.
A postal service charges one price for a small parcel, a higher price for a medium one and more again for a large one. There is no single formula for the cost, but there is a single rule: which formula applies depends on the weight.
You have written this shape of rule many times. Run one and check the boundaries carefully.
Change the first test from grams <= 100 to grams < 100 and run it again. Exactly one of the six lines changes, and it is the one at the boundary.
A function defined by different formulas on different stretches of the domain is called a piecewise function. Mathematics writes it with a single large brace, one line per piece, and the condition for that piece on the right.
The conditions must not overlap, or an input would have two outputs and the promise would break. They should also leave no gaps inside the intended domain, or some input would have none. The code enforces both automatically, because elif only runs when the earlier tests failed; on paper you have to check it yourself.
Here is a piecewise function with genuine formulas rather than constants. Evaluate it in your head first, then run it.
Change the second piece from x * x to x * x + 4 and run it again. The gap at the boundary gets wider; on a graph that is a visible jump.
Just under 1 the outputs are close to 5, and at 1 itself the output is 1. On a graph you draw the left piece up to a hollow circle at , showing that point is not part of the graph, and start the right piece with a filled circle at , showing that one is. Hollow for excluded, filled for included, exactly as with brackets in interval notation.
Here is that rule drawn, with the boundary on a slider so you can watch the break open and close. The straight part on the left is and the curved part on the right is .
- What the left piece is heading for
- 5
- What the right piece starts at
- 1
- Size of the jump
- 4
Drag c to 3 and the two pieces meet, so the jump closes and the size of the jump reads 0. Drag it to -1 and they meet again. At every other boundary the graph has a genuine break in it.
Absolute value is a piecewise rule in disguise
The absolute value of a number is its distance from zero, so and . Written as a rule that a machine could follow, it has two cases.
Change return -x to return x and run it again. The two columns now disagree on exactly the negative inputs.
That is why the graph of is a V. To the right of the origin it is the line , to the left it is the line , and the two meet at a corner.
Step functions
Some rules do not change at two or three places but at every whole number. A car park that charges by each started hour is one. Run it.
Change math.ceil to math.floor and run it again. One of these two is the rule a car park uses, and the other one gives away free parking.
A function whose graph is a series of flat pieces with jumps between them is a step function. The two standard ones are the floor, written , which rounds down to the nearest whole number, and the ceiling, written , which rounds up. So and .
A step function is worth seeing as well as evaluating, because the shape is what gives it the name. The graph below is the car park charge from the snippet above.
- Cost of a stay of 2.5 hours
- 9
- Cost of a stay of exactly 3 hours
- 9
- Cost of a stay of 3.01 hours
- 12
- Cost of a stay of 5 hours
- 15
Drag r and watch every step change height together. The near-vertical lines joining one step to the next are drawn by the plotter and are not part of the graph: at each whole number the function has one output, not a whole column of them.
For the function that is when and when , what are and ?
, because 1 satisfies , so the second piece applies and . , because 0 satisfies , so the first piece applies and .