# functions + graphs

> what a function is, how its graph moves when you change it, and how to undo one.

source: https://learning.anthnydas.com/math/functions/

## tl;dr

a function turns each input into one output. its graph is every (x, f(x)) pair drawn at once, and an edit to the formula moves that picture in a way you can read off.

- **up k:** f(x) + k (outside the function acts on y, as written)
- **right h:** f(x − h) (inside acts on x, backwards. the minus moves it right)
- **stretch by a:** a · f(x) (a negative a flips it over the x axis)
- **squeeze by 1/b:** f(bx) (toward the y axis. a negative b flips it over the y axis)
- **compose:** f(g(x)) (g runs first, then f. the other order differs)
- **inverse:** f⁻¹(f(x)) = x (not 1/f(x). only a one-to-one function has one)
- **reach for it:** sketching y = 2(x − 1)² − 3 without plotting points, reading a graph back into a formula, solving for the input: undo the function, the next pages, where exponents, logs and trig are graphs that move the same way

## the idea

a function is a rule that takes an input and gives back exactly one output. $f(x)$ names it: $f$ is the rule, $x$ is what goes in, and $f(x)$ is what comes out. $f(x) = x^2 - 1$ says square the input and subtract 1, so $f(3) = 8$, and $f(-3) = 8$ too. two inputs may share an output. one input never gets two.

the same function shows up three ways: a formula, a table of its values, and a graph, which is every point $(x, f(x))$ drawn at once.

| $x$       | −2 | −1 | 0  | 1 | 2 |
| --------- | -- | -- | -- | - | - |
| $x^2 - 1$ | 3  | 0  | −1 | 0 | 3 |

the graph is the table with the gaps filled in: a parabola with its lowest point at $(0, -1)$. edit the formula and the picture moves, and the move can be read straight off the edit. the movie makes three edits to $y = x^2$, one at a time.

> **interactive figure:** an animation on a plot from x = −6 to 6 and y = −4 to 5: y = x² turns into y = 2(x − 1)² − 3 one move at a time, with the vertex marked and the parent y = x² left behind, dashed. first the parabola slides right 1 and the vertex goes from (0, 0) to (1, 0). then every height doubles, stretching it up away from the x axis, and the vertex stays at (1, 0). then the whole graph drops 3 and the vertex lands at (1, −3).

start with $y = x^2$: every point is $(x, x^2)$, and the lowest one, the vertex, sits at $(0, 0)$.

replace $x$ with $x - 1$: $y = (x - 1)^2$. the graph slides right by 1. what happened at $x = 0$ now happens at $x = 1$, so the vertex is at $(1, 0)$.

multiply by 2: $y = 2(x - 1)^2$. every height doubles, so the curve stretches up, away from the x axis. the vertex stays put: twice 0 is 0.

subtract 3: $y = 2(x - 1)^2 - 3$. the whole graph drops 3 and the vertex lands at $(1, -3)$. three edits, three moves, read straight off the formula: right 1, stretched by 2, down 3.

functions are all over this site. the growth curves on the [big o](/algos/big-o/) page are functions of $n$, and an ease in [gsap](/graphics/gsap/#easing) is a function from the share of the time that has passed to the share of the move that is done.

## explore

pick a shape and slide the four numbers in $y = a \cdot f(b(x - h)) + k$. the parent shape stays behind, dashed. the readout gives the equation, where two of the parent's points land, and the domain and range.

> **interactive figure:** a plot from x = −6 to 6 and y = −4 to 4 with a row of buttons for seven shapes, x, x², x³, √x, |x|, 1/x and 2ˣ, and four sliders for a, b, h and k in y = a·f(b(x − h)) + k. the parent shape stays behind, dashed, and the transformed graph is drawn over it with two points marked: where the parent's anchor lands, (h, k) for every shape but 2ˣ, whose (0, 1) lands at (h, a + k), and where its point at x = 1 lands, at (h + 1/b, a·f(1) + k). a readout gives the equation, the two points, and the domain and range. it opens on x² with a = 2, b = 1, h = 1 and k = −3: y = 2(x − 1)² − 3, the vertex at (1, −3), the point (1, 1) at (2, −1), domain all x, range y ≥ −3.

- $k$ moves the graph up, or down when it's negative. $h$ moves it right: $x - 1$ inside means right 1, and $x + 2$ means left 2
- $a$ stretches it away from the x axis, and a negative $a$ flips it over the x axis
- $b$ squeezes it toward the y axis by $1/b$, so $f(2x)$ is half as wide, and a negative $b$ flips it over the y axis
- for every shape, the point the parent had at $x = 0$ ends up at $x = h$, and the point it had at $x = 1$ ends up at $x = h + 1/b$

## try it

> **interactive figure:** a practice exercise with five kinds of question: pick the equation of a drawn graph, say where a moved shape's vertex, corner, start or center lands, work out f(g(n)) or g(f(n)) for two small functions, find f⁻¹ of a value, or give a formula's domain. it opens on the graph of y = |x + 2| − 1, then f(g(2)) for f(x) = x + 3 and g(x) = 2x, which is 7.

## the rules

### one output for each input

a function $f$ pairs each input $x$ in its domain with one output $f(x)$. the domain is the set of inputs it accepts, and the range is the set of outputs it reaches.

$$
f : x \mapsto f(x) \qquad f(x) = x^2 - 1 \;\Rightarrow\; f(3) = 8,\; f(-3) = 8
$$

why one output: $f(3)$ has to mean one number, or $f(3) + 1$ means nothing. on a graph this is the vertical line test. a vertical line is one input, so if it crosses the graph twice, that input has two outputs and the picture isn't a function's graph. a circle fails it. a parabola passes.

two things take inputs out of a domain: a square root of a negative number, and a division by zero. $\sqrt{x - 2}$ needs $x \ge 2$, and $\frac{1}{x + 1}$ needs $x \ne -1$. the range comes from the shape: $x^2 - 1$ is never below $-1$, so its range is $y \ge -1$.

### shapes to know by sight

seven parents, with their values at a few inputs and what each one takes and gives. the explore figure draws each one, and the next page is about the last.

| $f(x)$            | $x = -2$ | $-1$ | $0$ | $1$ | $2$  | domain    | range     |
| ----------------- | -------- | ---- | --- | --- | ---- | --------- | --------- |
| $x$               | −2       | −1   | 0   | 1   | 2    | all $x$   | all $y$   |
| $x^2$             | 4        | 1    | 0   | 1   | 4    | all $x$   | $y \ge 0$ |
| $x^3$             | −8       | −1   | 0   | 1   | 8    | all $x$   | all $y$   |
| $\sqrt{x}$        | –        | –    | 0   | 1   | 1.41 | $x \ge 0$ | $y \ge 0$ |
| $\lvert x \rvert$ | 2        | 1    | 0   | 1   | 2    | all $x$   | $y \ge 0$ |
| $1/x$             | −0.5     | −1   | –   | 1   | 0.5  | $x \ne 0$ | $y \ne 0$ |
| $2^x$             | 0.25     | 0.5  | 1   | 2   | 4    | all $x$   | $y > 0$   |

- $x^2$ and $\lvert x \rvert$ are even: $f(-x) = f(x)$, so each graph is its own mirror image across the y axis. $x^3$ and $1/x$ are odd: $f(-x) = -f(x)$
- $\sqrt{x}$ starts at the origin, only exists for $x \ge 0$, and climbs slower and slower. $2^x$ doubles with every step to the right and halves with every step to the left, so it never reaches 0
- $1/x$ has nothing at $x = 0$. its two arms hug the axes and never touch them

### transformations

$$
y = a \cdot f\big(b\,(x - h)\big) + k
$$

- **$f(x) + k$: up by $k$.** every output gains $k$, so every point of the graph rises by $k$
- **$f(x - h)$: right by $h$.** $f$ gets $x - h$, so it sees $0$ when $x = h$: what happened at $0$ now happens at $h$
- **$a \cdot f(x)$: stretched by $a$,** away from the x axis. every output is multiplied by $a$, and when $a < 0$ each one lands on the other side of the axis: the graph flips over the x axis
- **$f(bx)$: squeezed by $1/b$,** toward the y axis. $f$ gets $bx$, so it reaches the value it had at $x_0$ when $x = x_0 / b$. when $b < 0$ the graph flips over the y axis

outside the function, the changes act on $y$ as written. inside, they act on $x$, and backwards: a minus moves right, a 2 halves the width. the order matters too. $a \cdot f(x) + k$ multiplies first, then adds, so the stretch comes before the shift up: $2(x - 1)^2 - 3$ doubles the heights, then drops them by 3. inside, $f(b(x - h))$ squeezes first, then shifts: $f(2x - 4) = f(2(x - 2))$ moves right 2, not 4. factor the inside before you read it.

### composition

$$
(f \circ g)(x) = f(g(x))
$$

do $g$ first, then $f$. the inner function runs on the input, and the outer one only ever sees its result. with $f(x) = x + 3$ and $g(x) = 2x$: $f(g(2)) = f(4) = 7$, but $g(f(2)) = g(5) = 10$. in general $f(g(x)) = 2x + 3$ and $g(f(x)) = 2x + 6$, which are never equal.

the transformation rules are compositions. $f(x - h)$ is $f$ after the shift $x \mapsto x - h$, and $a f(x) + k$ is the map $y \mapsto ay + k$ after $f$. that is the whole reason inside and outside behave differently: a change inside alters what $f$ receives, and a change outside alters what $f$ returned.

### inverses

$f^{-1}$ undoes $f$:

$$
f^{-1}(f(x)) = x \qquad f(f^{-1}(y)) = y
$$

to find it, write $y = f(x)$, swap $x$ and $y$, and solve for $y$. the swap is why the graph of $f^{-1}$ is the graph of $f$ mirrored across the line $y = x$: every point $(a, b)$ on $f$ becomes $(b, a)$ on $f^{-1}$, and swapping coordinates is that mirror.

only a one-to-one function has an inverse. if two inputs share an output, the inverse would have to send that output back to both, and a function can't. the horizontal line test checks it: a horizontal line that crosses the graph twice means two inputs with one output. $x^2$ fails, since $3$ and $-3$ both give $9$. restricting the domain fixes it: $x^2$ on $x \ge 0$ passes, and its inverse is $\sqrt{x}$.

> **interactive figure:** a plot from x = −5 to 5 and y = −4 to 4 with the line y = x dashed and buttons for four functions: 2x + 1, x² on x ≥ 0, x³ and 2ˣ. the function is drawn in cyan and its inverse in amber, its mirror image across y = x. a point on the function can be dragged along it, or moved with the arrow keys, and its twin on the inverse follows with the coordinates swapped. it opens on x² on x ≥ 0 at (1.5, 2.25), with the twin at (2.25, 1.5), and the readout reads f(1.5) = 2.25 and f⁻¹(2.25) = 1.5. the left half of the parabola is drawn dashed: it is cut away so that each output comes from one input, and a dotted horizontal line shows the second input it would have given, −1.5.

## worked examples

### sketch y = −|x + 2| + 1

read the formula from the inside out, starting from $y = \lvert x \rvert$:

1. $x + 2$ is $x - (-2)$: left 2. the corner moves from $(0, 0)$ to $(-2, 0)$
2. the minus in front: every height is multiplied by $-1$, so the v flips over the x axis and opens down
3. $+ 1$: up 1. the corner lands at $(-2, 1)$

the arms follow the same moves: $(1, 1)$ lands at $(-1, 0)$ and $(-1, 1)$ at $(-3, 0)$, so the graph crosses the x axis at $-1$ and $-3$. domain: all $x$. range: $y \le 1$, since the corner is now the top.

### read a graph back

a parabola has its vertex at $(3, -2)$ and passes through $(4, 0)$. the vertex gives $h$ and $k$:

$$
y = a(x - 3)^2 - 2
$$

the other point gives $a$. at $x = 4$: $0 = a \cdot 1^2 - 2$, so $a = 2$ and $y = 2(x - 3)^2 - 2$. check: $2(4 - 3)^2 - 2 = 0$.

### compose two functions

$f(x) = x + 3$ and $g(x) = 2x$. for $f(g(5))$, $g$ runs first: $g(5) = 10$, then $f(10) = 13$. for $g(f(5))$: $f(5) = 8$, then $g(8) = 16$. as formulas, $f(g(x)) = 2x + 3$ and $g(f(x)) = 2(x + 3) = 2x + 6$.

### find an inverse

$f(x) = 2x - 1$. write $y = 2x - 1$, swap: $x = 2y - 1$, solve: $y = \frac{x + 1}{2}$. so $f^{-1}(x) = \frac{x + 1}{2}$. check that it undoes $f$: $f(4) = 7$ and $f^{-1}(7) = 4$. and $1/f(4) = 1/7 \approx 0.14$ is something else entirely: the $-1$ in $f^{-1}$ is a name, not a power.

### restrict a domain

$f(x) = x^2 - 1$ has no inverse on all of $x$: $f(3) = f(-3) = 8$. on $x \ge 0$ it does. swap and solve: $x = y^2 - 1$, so $y = \sqrt{x + 1}$. its domain is $x \ge -1$, the old range, and its range is $y \ge 0$, the old domain. an inverse swaps those too.

## the key idea up close

### why f(x − h) moves right

let $g(x) = f(x - h)$ and feed it the input $x + h$:

$$
g(x + h) = f(x + h - h) = f(x)
$$

whatever $f$ does at $x$, $g$ does at $x + h$. so every point $(x, f(x))$ of $f$'s graph shows up on $g$'s graph as $(x + h, f(x))$: the same height, $h$ further right. the minus inside is a delay. $g$ has to wait until its input reaches $h$ before it sees what $f$ saw at $0$. with $f(x) = x^2$ and $h = 1$: $f$ is $0$ at $x = 0$, and $g(x) = (x - 1)^2$ is $0$ at $x = 1$.

the same argument covers $b$. $g(x) = f(bx)$ has $g(x / b) = f(x)$, so $f$'s point at $x$ lands at $x / b$: closer to the y axis when $\lvert b \rvert > 1$, and on the other side of it when $b < 0$. changes outside the function need no argument at all. they act on the output directly, so $(x, y)$ goes to $(x, ay + k)$.

in one line, the point $(x, y)$ of $f$'s graph lands at

$$
\Big(h + \frac{x}{b},\; a\,y + k\Big)
$$

that is what the explore figure's readout computes, and the first half is why the inside reads backwards: $x$ is divided by $b$ and has $h$ added, the reverse of what the formula does to it.

## pitfalls

- **$f(x - 2)$ moves right, not left.** $f$ sees $0$ when $x = 2$, so what was at the origin is now at $2$. the same way, $f(x + 2)$ moves left 2
- **$f^{-1}(x)$ isn't $1/f(x)$.** with $f(x) = 2x - 1$, $f^{-1}(7) = 4$ while $1/f(7) = 1/13$. the $-1$ is a label for the function that undoes $f$
- **$f(g(x))$ and $g(f(x))$ differ.** $f(g(2)) = 7$ and $g(f(2)) = 10$ above. check which one runs first before you compute
- **one output per input, but not the other way.** $f(3) = f(-3) = 8$ is fine for $x^2 - 1$. a circle isn't a function's graph, since a vertical line through it hits two points
- **domain gaps.** a square root of a negative and a division by zero are the two to look for: $\sqrt{x - 2}$ needs $x \ge 2$ and $1/(x + 1)$ needs $x \ne -1$. in $f(g(x))$, $g(x)$ also has to land in $f$'s domain
- **a squeeze inside can look like a stretch outside.** $(2x)^2 = 4x^2$, so for $x^2$ a squeeze by 2 and a stretch by 4 draw the same graph. for most functions they differ: $\sqrt{4x} = 2\sqrt{x}$, not $4\sqrt{x}$, and $(2x)^3 = 8x^3$
- **factor the inside.** $f(2x - 4)$ is $f(2(x - 2))$: squeezed by 2, then right 2, not right 4

## practice

1. $f(x) = x^2 - 1$. find $f(3)$ and $f(-3)$, and say why a function may do that.

   **answer:** $f(3) = 9 - 1 = 8$ and $f(-3) = 9 - 1 = 8$. two inputs sharing an output is allowed: the rule only promises one output for each input, not one input for each output. it is the other direction, two outputs for one input, that a function can't do.

2. give the domain of $g(x) = \sqrt{x - 2} + \dfrac{1}{x - 5}$.

   **answer:** the square root needs $x - 2 \ge 0$, so $x \ge 2$. the fraction needs $x - 5 \ne 0$, so $x \ne 5$. both at once: $x \ge 2$ and $x \ne 5$.

3. describe how $y = -2(x + 1)^2 + 4$ comes from $y = x^2$, give its vertex, and say where the parent's point $(1, 1)$ lands.

   **answer:** inside, $x + 1$: left 1. outside, $-2$: stretched by 2 and flipped over the x axis. then $+ 4$: up 4. the vertex goes from $(0, 0)$ to $(-1, 4)$, and the parabola opens down. the point $(1, 1)$ lands at $(1 - 1,\; -2 \cdot 1 + 4) = (0, 2)$.

4. a parabola has its vertex at $(3, -2)$ and passes through $(4, 0)$. write its equation.

   **answer:** $y = a(x - 3)^2 - 2$ from the vertex. at $(4, 0)$: $0 = a(4 - 3)^2 - 2 = a - 2$, so $a = 2$. the equation is $y = 2(x - 3)^2 - 2$.

5. $f(x) = x + 3$ and $g(x) = 2x$. find $f(g(5))$ and $g(f(5))$, then find every $x$ with $f(g(x)) = g(f(x))$.

   **answer:** $f(g(5)) = f(10) = 13$ and $g(f(5)) = g(8) = 16$. in general $f(g(x)) = 2x + 3$ and $g(f(x)) = 2x + 6$. setting them equal gives $3 = 6$, which never holds, so there is no such $x$: for this pair the order always matters.

6. $f(x) = 3x - 2$. find $f^{-1}(x)$ and $f^{-1}(7)$, and check it. then say whether $k(x) = x^2 - 1$ has an inverse, and give one on a restricted domain.

   **answer:** swap and solve: $x = 3y - 2$, so $y = \dfrac{x + 2}{3}$ and $f^{-1}(x) = \dfrac{x + 2}{3}$. then $f^{-1}(7) = 3$, and the check: $f(3) = 9 - 2 = 7$.

   $k$ has no inverse on all $x$, since $k(3) = k(-3) = 8$ fails the horizontal line test. on $x \ge 0$ it does: $x = y^2 - 1$ gives $k^{-1}(x) = \sqrt{x + 1}$, with domain $x \ge -1$ and range $y \ge 0$.

## related

- https://learning.anthnydas.com/math/exponents-logs.md
- https://learning.anthnydas.com/math/trig.md
- https://learning.anthnydas.com/math/limits.md
- https://learning.anthnydas.com/math/derivatives.md
- https://learning.anthnydas.com/algos/big-o.md
- https://learning.anthnydas.com/graphics/gsap.md
