Piecewise Modeling Lab

Use two linear rules split at x = c, then evaluate values and jump behavior.

Loading interactive simulation...

Lesson

The theory — Piecewise Modeling Lab

A piecewise function is not one rule with an exception; it is two complete rules plus a statement of which one applies where. Here one linear rule governs everything below a breakpoint c and a second governs everything from c upwards. The interesting question is never what each piece does — they are just straight lines — but what happens at the seam.

What each symbol means

c
the breakpoint: the input at which the rule changes.
f(c⁻)
where the left piece is heading as x approaches c from below. It is a limit, not a value: at c the left rule no longer applies.
f(c⁺)
the value the right piece actually takes at c, which is also the value of the function there.
jump
f(c⁺) − f(c⁻). Zero exactly when the two pieces meet.

Where the formula comes from

  1. Write the definition out with its condition attached: f(x) = m₁x + b₁ for x < c, and f(x) = m₂x + b₂ for x ≥ c. Note which piece owns the breakpoint itself — here the right one, which is why the graph shows a hollow circle on the left branch and a filled one on the right.
  2. At c the function has a value from the right rule, m₂c + b₂. The left rule has no value there, but it does have a destination: as x climbs towards c, m₁x + b₁ approaches m₁c + b₁. That is the left-hand limit.
  3. Continuity at a point means the limit from each side and the value all agree. The right-hand limit and the value are the same thing here, so two of the three are automatic and the whole condition collapses to one equation: m₁c + b₁ = m₂c + b₂.
  4. One equation, and any single coefficient can be moved to satisfy it: b₂ = (m₁ − m₂)c + b₁. So continuity is always available and never free — you have to spend a parameter on it. When you do not, the leftover is exactly what the readout calls the jump: (m₂c + b₂) − (m₁c + b₁).

How to read what you see

The blue segment is the left rule and the orange one the right, and they are drawn as far as their own condition allows. At the breakpoint the blue branch ends in a hollow circle — a destination it never occupies — and the orange one starts with a filled dot, which is the actual value of f(c). When the two coincide the circles sit on top of each other and the jump row reads 0. The teal dot is f(x₀) for whatever you put in the evaluate box, and the last three rows turn the picture into numbers: whether the pieces meet, how much the slope changes as you cross, and whether the step down creates a stretch of inputs that cost more than the breakpoint does.

Assumes
Both pieces are straight lines, there is exactly one breakpoint, and it sits at a known place. The half-open convention matters and is a choice: the right rule owns x = c, so f(c) is m₂c + b₂. Flip that convention and the value at the single point c changes while the jump does not.
Breaks when
The real trouble comes when c is not known but fitted. A breakpoint estimated from the same data used to fit the lines is a parameter chosen to minimise the error, so the ordinary regression standard errors are too small and the fit looks better than it is — this is what segmented-regression methods exist to handle. And a two-piece model says nothing at all about a third regime: extrapolating either line beyond the range you observed assumes a rule you never tested.

Continuity at the breakpoint has to be engineered 🖖

The jump row is the one to watch, and three of the four presets print a non-zero value into it. The taxi drops from 13 to 12 at the breakpoint, the discount by 2.5, the cooling curve by 4.8. Every one of those falls, so crossing into the cheaper rate makes the total go down. Continuity is not the default here but an exact coincidence you have to arrange: it needs m₁c + b₁ = m₂c + b₂. The tax preset is the one that satisfies it, and it does so deliberately — its second intercept is −2 because that is the continuity condition solved for b₂. Set the taxi preset’s second intercept from 8 to 9 and its jump reads 0 too.

One function, two rulebooks 🖖

A piecewise model uses one linear rule below the breakpoint c and a different one above it — like a taxi meter that charges one rate at first and a cheaper rate later. The two pieces meet smoothly only when they agree at c: continuous exactly when m1·c + b1 = m2·c + b2. Otherwise the readout shows a jump — the graph teleports up or down at the seam.

The kink that powers deep learning 🖖

The activation function behind most modern neural networks, ReLU, is just a piecewise linear function: slope 0 while x is negative and slope 1 once it turns positive, joined at the breakpoint x = 0. It is continuous there yet has no derivative — a sharp corner, not a smooth curve. Stack millions of these units and the whole network still computes one enormous continuous piecewise-linear function.

A discount can make eight cost more than ten 🖖

Load the bulk-discount preset and look at the last row of the readout. The rate falls from 1.2 to 0.7 at ten units and the total falls with it, so ten units cost 9.5 while eight cost 9.6. The row gives the whole region: from 7.916667 units up to the breakpoint you pay more than you would by simply ordering ten. Every downward jump creates such an interval, and it is a strange thing to write into a price list, because it pays a customer who wants eight to order ten and take the extra two for nothing. That is the practical reason to check continuity rather than a decorative one. Switch to the cooling preset and the row says there is no such region — a falling branch never rises above the value it is heading towards.

Problem solved in full

  1. A tariff breakpoint at 5 and the range where buying more costs less 6 steps

    A tariff charges 2x + 3 below a breakpoint at 5 and 0.8x + 8 above it. Find the value at 7 — then find the range where buying more costs less.

    1. Two straight lines and a rule for which applies. The only care needed is the boundary: at exactly x = 5 the second branch is the one in force.

    2. Approach the breakpoint from both sides. The left limit and the actual value disagree, and the size of the disagreement is the jump.

    3. The slopes disagree too, and independently. A function can be continuous with a kink, or jump with no kink; this one does both.

    4. Now the interesting question. Ask where the first branch has already climbed above the value the second branch starts at.

    5. That inequality holds from 4.5 up to the breakpoint, and on that whole interval the cheaper purchase is the larger one.

    6. The condition that would remove the window is worth writing down, because it is what a well-designed schedule satisfies.

    Answer

    13.6, and between 4.5 and 5 you should always buy 5. The second branch starts at 12 while the first branch is still climbing past it, so anyone in that window pays more for less. The window is not a rounding artefact — it is 10% of the whole first bracket, and it is exactly what a discontinuous tariff buys you. Real schedules avoid it by making the pieces meet: continuity requires b₂ − b₁ = (m₁ − m₂)c, which here would need 5 to equal 6. Marginal tax brackets are built that way on purpose, which is why the folk belief that a raise can lower your take-home pay is false for income tax and true for almost every bulk discount ever printed.

References (2)

Example problems

  • taxi pricing model - 2x + 3 below the breakpoint at 5 and 0.8x + 8 above, so the value drops from 13 to 12 as you cross. Set that second intercept to 9 instead of 8 and the jump closes to zero.
  • bulk discount model - 1.2 per unit up to ten, then 0.7 with a 2.5 intercept: ten units cost 9.50 and eight cost 9.60. Everything from 7.916667 units up is worse than simply ordering ten.
  • cooling phases - A falling model, -0.5 then -0.1, dropping 4.8 at the breakpoint. The only preset where the tool reports no overpaying region at all, because a falling branch has nothing to rise above.
  • marginal tax bracket - The one preset with no step: 20% up to 10 and 40% above, with the second intercept at −2 so the pieces meet exactly