Int 3 for Programmers

Functions, transformations, composition and inverses

7 / 396
Contents

Contents

Int 3 for Programmers

0 of 396 complete

10 min read

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.

Try CodePython

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.

C(w)={20<w≤1005100<w≤5009w>500C(w) = \begin{cases} 2 & 0 < w \le 100 \\ 5 & 100 < w \le 500 \\ 9 & w > 500 \end{cases}
The same rule the code carries out. Each line is one piece, with the inputs it applies to beside it.

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.

Try CodePython

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 (1,5)(1, 5), showing that point is not part of the graph, and start the right piece with a filled circle at (1,1)(1, 1), 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 2x+32x + 3 and the curved part on the right is x2x^2.

A two-piece rule, drawn
f(x)={2x+3x<1x2x≥1f(x) = \begin{cases} 2x + 3 & x < 1 \\ x^2 & x \ge 1 \end{cases}
-6-0.5510.516-4-2024
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 ∣5∣=5|5| = 5 and ∣−5∣=5|-5| = 5. Written as a rule that a machine could follow, it has two cases.

Try CodePython

Change return -x to return x and run it again. The two columns now disagree on exactly the negative inputs.

∣x∣={xx≥0−xx<0|x| = \begin{cases} x & x \ge 0 \\ -x & x < 0 \end{cases}
The minus sign in the second line does not make the answer negative; it undoes a negative.

That is why the graph of y=∣x∣y = |x| is a V. To the right of the origin it is the line y=xy = x, to the left it is the line y=−xy = -x, 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.

Try CodePython

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 ⌊x⌋\lfloor x \rfloor, which rounds down to the nearest whole number, and the ceiling, written ⌈x⌉\lceil x \rceil, which rounds up. So ⌊2.7⌋=2\lfloor 2.7 \rfloor = 2 and ⌈2.1⌉=3\lceil 2.1 \rceil = 3.

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.

The staircase of a step function
C(x)=3⌈x⌉C(x) = 3\lceil x \rceil
06.51319.52601.252.53.755
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 2x+32x + 3 when x<1x < 1 and x2x^2 when x≥1x \ge 1, what are f(1)f(1) and f(0)f(0)?

f(1)=1f(1) = 1, because 1 satisfies x≥1x \ge 1, so the second piece applies and 12=11^2 = 1. f(0)=3f(0) = 3, because 0 satisfies x<1x < 1, so the first piece applies and 2(0)+3=32(0) + 3 = 3.

Check yourself

A phone plan charges 20 pence for each started minute. Which rule gives the cost in pence for a call of tt minutes?

All lessons in Int 3 for Programmers