Function Composition: Machines in a Row

Category: Functions   Level: 201

(f ∘ g)(x) = f(g(x)) = (x + 1)2, (g ∘ f)(x) = x2 + 1

Composition feeds the output of one function into another. The machine you reach first runs first, and swapping the order usually changes the answer.

Step through it

Watch it

Animated walkthrough

Step by step

Step 1: Line up two machines

Machine g adds one. Machine f squares. Put g first and f second, so the output of g drops straight into f.

g(x) = x + 1, f(x) = x2

Step 1: Line up two machines

Step 2: Run the number 3

Feed in 3. Machine g gives 4, and f turns 4 into 16. Read f(g(3)) from the inside out, the same order the machines run.

(f ∘ g)(3) = f(g(3)) = f(4) = 16

Step 2: Run the number 3

Step 3: Run any x

Feed in a plain x instead. Machine g hands over x + 1, and f squares the whole package.

(f ∘ g)(x) = (x + 1)2

Step 3: Run any x

Step 4: Swap the order

Now f runs first. Three squared is 9, and 9 + 1 is 10, not 16. Same machines, different line, different result.

(g ∘ f)(3) = g(f(3)) = g(9) = 10

Step 4: Swap the order

Step 5: Compare the formulas

In general (x + 1)² and x² + 1 differ, so order matters. Putting on socks, then shoes, beats the reverse.

(x + 1)2 = x2 + 2x + 1 ≠ x2 + 1

Step 5: Compare the formulas

All steps at once

Storyboard

Generate it yourself

cd svg-generator
python3 -m svgmath render function-composition --out /tmp/function-composition

Source: svgmath/diagrams/function_operations.py