Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools

Composition of Functions Calculator

How to use
  1. Type the outer function in the f(x)f(x) box and the inner one in the g(x)g(x) box, for example f(x)=x2+1f(x) = x^2 + 1 and g(x)=xg(x) = \sqrt{x}. Learn more about entering the functions
  2. Read both compositions: f after g is f(g(x))f(g(x)) and g after f is g(f(x))g(f(x)). Each has its domain below it. Learn more about reading the answer
  3. Look at the graph: f(g(x))f(g(x)) is navy and g(f(x))g(f(x)) is amber. The two starting functions are drawn thin and dashed.
  4. Type a number in Trace x = to follow one input through both compositions, step by step.

Composition
f after g
(f∘g)(x)=x2+1\displaystyle (f \circ g)(x) = {\sqrt{x}}^{2}+1
g after f
(g∘f)(x)=x2+1\displaystyle (g \circ f)(x) = \sqrt{{x}^{2}+1}
Domain of f ∘ g
[0, ∞)\displaystyle [0,\ \infty)
Domain of g ∘ f
R\displaystyle \mathbb{R}
How it was found

f(x)=x2+1f(x) = {x}^{2}+1 and g(x)=xg(x) = \sqrt{x}.

For f∘gf \circ g, put g(x)g(x) in place of every xx in ff: f(g(x))=x2+1f(g(x)) = {\sqrt{x}}^{2}+1.

For g∘fg \circ f, put f(x)f(x) in place of every xx in gg: g(f(x))=x2+1g(f(x)) = \sqrt{{x}^{2}+1}.

The order matters: the two results are usually different functions. A composition is defined only where the inner function is defined and its output is allowed in the outer one.

At x=4x = 4: g(4)=2g(4) = 2, then f(2)=5f(2) = 5; the other way, f(4)=17f(4) = 17, then g(17)=4.1231g(17) = 4.1231.

Drawn by the tool
g(x)=xg\left(x\right)=\sqrt{x}read-only
f(g(x))=x2+1f(g(x)) = {\sqrt{x}}^{2}+1read-only
g(f(x))=x2+1g(f(x)) = \sqrt{{x}^{2}+1}read-only
f(x)=x2+1f\left(x\right)={x}^{2}+1
g(x)=xg\left(x\right)=\sqrt{x}
f(g(x))=x2+1f(g(x)) = {\sqrt{x}}^{2}+1
g(f(x))=x2+1g(f(x)) = \sqrt{{x}^{2}+1}


Composing two functions

DemoCompose two functions
Step 0 of 4
    Type one function in the f(x)f(x) box and another in the g(x)g(x) box. The calculator builds both compositions at once:

  • f∘gf \circ g, read "ff after gg": gg acts first, then ff acts on the result. Its formula is f(g(x))f(g(x)).
  • g∘fg \circ f, read "gg after ff": ff acts first, then gg. Its formula is g(f(x))g(f(x)).

  • Each formula is written first as the plain substitution, and then, when that reads better, in a tidied form after an equals sign.

    The Trace x = box follows a single number through both compositions. The last line of How it was found shows the intermediate value and the final one for each order, and the two results are marked on the graph.

Reading the answer

DemoThe domain of a composition
Step 0 of 4
    There are four boxes:

  • f after g: the formula of (f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x)).
  • g after f: the formula of (g∘f)(x)=g(f(x))(g \circ f)(x) = g(f(x)).
  • Domain of f ∘ g and Domain of g ∘ f: the inputs each composition accepts.

  • On the graph, f(g(x))f(g(x)) is the navy curve and g(f(x))g(f(x)) is the amber one. The original ff and gg are thin dashed curves, so you can see what the compositions were built from.

    When the traced number is outside a domain, the trace says undefined at the step where it fails. That is the quickest way to see why a number is left out.

What a composition is

To compose two functions is to feed the output of one into the other. The composition f∘gf \circ g is the function

(f∘g)(x)=f(g(x)).(f \circ g)(x) = f(g(x)).


The function written on the right, next to xx, acts first. With f(x)=2x+1f(x) = 2x + 1 and g(x)=x2g(x) = x^2: to find (f∘g)(2)(f \circ g)(2), first g(2)=4g(2) = 4, then f(4)=9f(4) = 9.

As a formula, put the whole of g(x)g(x) in place of every xx in ff: f(g(x))=2(x2)+1=2x2+1f(g(x)) = 2(x^2) + 1 = 2x^2 + 1.

The order matters

Swapping the order usually gives a different function. With the same f(x)=2x+1f(x) = 2x + 1 and g(x)=x2g(x) = x^2:

f(g(x))=2x2+1,g(f(x))=(2x+1)2=4x2+4x+1.f(g(x)) = 2x^2 + 1, \qquad g(f(x)) = (2x + 1)^2 = 4x^2 + 4x + 1.


At x=2x = 2 the first gives 99 and the second gives 2525.

Putting on socks and then shoes is not the same as shoes and then socks. In the same way, "square, then double and add one" is not "double and add one, then square".

Some pairs do give the same result in both orders. The most important case: a function and its inverse, where both orders return xx.
f(g(x))=2x2+1f(g(x)) = 2x^2 + 1 (navy) and g(f(x))=(2x+1)2g(f(x)) = (2x + 1)^2 (amber) are different functions.

The domain of a composition

A number xx is in the domain of f∘gf \circ g when two things hold:

1. xx is in the domain of gg, so that g(x)g(x) exists;
2. g(x)g(x) is in the domain of ff, so that ff can take it.

With f(x)=xf(x) = \sqrt{x} and g(x)=x2−4g(x) = x^2 - 4: gg accepts everything, but ff needs g(x)≥0g(x) \geq 0, that is x2≥4x^2 \geq 4. The domain of f∘gf \circ g is (−∞, −2]∪[2, ∞)(-\infty,\ -2] \cup [2,\ \infty).

The other order, g(f(x))=(x)2−4g(f(x)) = (\sqrt{x})^2 - 4, needs x≥0x \geq 0 for the inner root. Its formula simplifies to x−4x - 4, but its domain stays [0, ∞)[0,\ \infty): the domain is decided before simplifying.
x2−4\sqrt{x^2 - 4} exists only where x2−4≥0x^2 - 4 \geq 0: left of −2-2 and right of 22.

Composition with a simple inner or outer function

Many transformations are compositions with a line.

Let g(x)=x−2g(x) = x - 2 and f(x)=1xf(x) = \dfrac{1}{x}. Then f(g(x))=1x−2f(g(x)) = \dfrac{1}{x - 2}: the inner function changes the input, and the graph of ff moves 22 to the right. And g(f(x))=1x−2g(f(x)) = \dfrac{1}{x} - 2: the outer function changes the output, and the graph moves 22 down.

The same pattern holds in general. A change made inside acts on xx and moves the graph sideways; a change made outside acts on the value and moves the graph up or down.
1x\frac{1}{x} (dashed): changed inside it moves right (navy), changed outside it moves down (amber).

Reading a formula as a composition

Going the other way is just as useful: a complicated formula can be split into an inner and an outer function.

x2+1\sqrt{x^2 + 1} is f(g(x))f(g(x)) with inner g(x)=x2+1g(x) = x^2 + 1 and outer f(x)=xf(x) = \sqrt{x}. sin⁡(3x)\sin(3x) has inner 3x3x and outer sin⁡\sin. (2x−5)4(2x - 5)^4 has inner 2x−52x - 5 and outer x4x^4.

To find the pieces, ask what is done last when you evaluate the formula at a number: that last step is the outer function. This reading is exactly what the chain rule of calculus needs.

Common mistakes

  • Multiplying instead of composing. (f∘g)(x)(f \circ g)(x) is f(g(x))f(g(x)), not f(x)⋅g(x)f(x) \cdot g(x).
  • Reading the order backwards. In f∘gf \circ g, the function on the right, gg, acts first.
  • Assuming the order does not matter. f(g(x))f(g(x)) and g(f(x))g(f(x)) are usually different.
  • Substituting into only one xx. Every xx in the outer function is replaced: for f(x)=x2+xf(x) = x^2 + x, f(g(x))=g(x)2+g(x)f(g(x)) = g(x)^2 + g(x).
  • Finding the domain from the simplified formula. (x)2(\sqrt{x})^2 simplifies to xx but is defined only for x≥0x \geq 0.