Function Composition Calculator

Enter f(x), g(x), and x0 to compose functions, compare order, and see each evaluation step.

Loading interactive simulation...

Swapping the order changes the answer, not just the notation 🖖

Composition is not commutative, and the gap is not subtle. Take f(x) = x² and g(x) = 2x + 3 at x = 2, which is one of this tool’s presets. Going through g first gives f(g(2)) = f(7) = 49. Going through f first gives g(f(2)) = g(4) = 11. Same two functions, same input, and the answers differ by 38. That is why the swap button here is worth pressing rather than assuming: f∘g and g∘f are genuinely different functions, and the ordinary rule that a × b equals b × a does not carry over.

Two machines in a row 🖖

Composing functions just means running them back to back: f(g(x)) feeds x into g first, then pours g's answer into f. Think of an assembly line where each station reshapes the part before passing it on. Always evaluate from the innermost parentheses outward, so g(x0) comes before f.

The fractal hiding in repeated composition 🖖

Compose one simple function with itself over and over and you leave algebra for chaos theory. Iterating z ↦ z² + c — the same machine feeding its own output back in — is the exact rule that draws the Mandelbrot set: a point belongs only if that endless self-composition stays bounded. So the humble f(g(x)) button is one step away from generating infinitely detailed fractals.

Common misconception

Matching at one x-value does not prove two composed functions are equal. To show f(g(x)) = g(f(x)) as functions, they must match throughout their shared domain.

Problems solved in full

  1. Compositions of f(x) = x² and g(x) = 2x + 3 5 steps

    f(x) = x², g(x) = 2x + 3, evaluated at x₀ = 2. Compose in both orders, then settle the harder question the panel leaves open: is the mismatch an accident of picking x₀ = 2, or do these two compositions disagree at every x?

    1. Compose symbolically before you compose numerically. In f∘g the inner function runs first, so the entire expression 2x + 3 is what gets squared — not x, and not the 3 on its own.

    2. Reverse the order and the squaring happens first, so it is x² that gets doubled and shifted. Both results are quadratics, but only one of them has a linear term, and that term is the whole disagreement.

    3. Substitute x₀ = 2 into each. The intermediate values are what to watch: 7 goes into the outer function in one order and 4 goes in for the other, so the outer function is being asked a different question each time. The panel prints every one of them — 7 and 4 on the way in, 49 and 11 on the way out.

    4. Subtracting turns a comparison at one point into a function you can interrogate everywhere. Its value at 2 is 38, which is 49 − 11, so nothing has been lost in the algebra.

    5. The gap is a quadratic with a positive x² coefficient, so it can vanish at most twice — and the discriminant 36 − 12 is positive, so it does vanish twice. Solving is the only step left.

    Answer

    49 against 11, a gap of 38, with 7 and 4 as the intermediates the panel prints alongside them. The stronger result is the one the panel cannot reach: across the whole real line these two compositions agree at exactly 2 points, x = −3 ± √6, which is −0.5505 and −5.4495. Only the first falls inside the −3 to 3 window the graph draws. So x₀ = 2 was not a lucky choice — almost every x₀ would have produced a mismatch, and the panel's verdict of 'not equal' is the answer it would give at all but 2 inputs. They also cross rather than touch, and the chain rule is what shows it: differentiating the gap gives 2(2x + 3)·2 − 2·2x = 4x + 12, which at the two roots is +9.798 and −9.798 — equal in size, opposite in sign, because between the roots it is g∘f that runs higher, by 12 at x = −3 where the graph window begins.

  2. Domain of compositions for f(x) = √x and g(x) = x² − 1 5 steps

    f(x) = √x, g(x) = x² − 1, at x₀ = 0.5. One order returns a number and the other returns nothing at all. Work out the domain of each composition, and then find every x at which the two of them agree.

    1. Follow f∘g through the middle and watch where it dies. 0.5 is a perfectly legal input to g, and −0.75 is a perfectly legal output of g. What is illegal is the handoff: −0.75 is not something you can take a square root of. The panel prints −0.75 and then has no number to print for the composition itself.

    2. The other order survives the same handoff, because √0.5 = 0.707107 is a legal input to g. Both of those figures are on the panel, and neither composition has been given a different x₀.

    3. Write the two domains down properly, working from the inside out. f∘g demands that g's output be non-negative, which is a condition on x²; g∘f demands only that x itself is square-rootable.

    4. On its own domain each composition simplifies, and one of them simplifies startlingly: squaring a square root undoes it, so g∘f is a straight line. Composing 2 curved functions has produced something with no curve in it.

    5. Where both are defined, ask when they are equal. The x² cancels on both sides and what is left is linear, so there is at most 1 solution.

    Answer

    f(g(0.5)) has no value while g(f(0.5)) = −0.5, with −0.75 and 0.707107 on the panel as the two intermediates that decided it. Order changed the domain here before it changed any value, and that is the difference no choice of x₀ can repair: f∘g exists only where |x| ≥ 1 and g∘f only where x ≥ 0, so they share just [1, ∞). On the −3 to 3 window the graph draws, the f∘g curve is simply absent for a full unit either side of 0 while the g∘f line runs straight through. Where they do overlap they meet exactly once, at x = 1, both equal to 0, and then separate at once: by x = 3 they read √8 = 2.828 and 2. 'Composition is not commutative' is usually taught as a claim about values. Here it is a claim about which questions can be asked at all.

References (1)

Example problems

  • linear after quadratic - 2x + 3 after x² - 1, at x = 2: f(3) = 9 one way and g(7) = 48 the other. Same pair of functions, a gap of 39.
  • quadratic after linear - x² after 2x + 3, at x = 2: f(7) = 49 one way and g(4) = 11 the other, a gap of 38. The worked problem then asks whether the two orders ever agree.
  • square after sine - f(g(1)) squares the sine: sin²(1) = 0.7081. g(f(1)) takes the sine of the square, and since 1² = 1 that is just sin(1) = 0.8415.
  • domain example - The only preset where one order has no value at all: g(0.5) = -0.75 and the square root of that is undefined, while the reverse order gives -0.5. Order can decide whether an answer exists.
  • same at one point - x² composed with the identity — both orders give x², because the identity is the one function composition leaves everything unchanged by