---
title: Inverse functions and atan2
heading: Undoing a function, and the angle in the right quadrant
eyebrow: A2 · Lesson 23 · Inverse functions and atan2
xp: 40
---

::: hook
A point sits at $x = -3$, $y = 4$. What angle does it make with the positive horizontal axis? Type $\tan^{-1}(4/-3)$ into a calculator and it says −53.1°, which points down and to the right. The point is plainly up and to the left. The calculator didn't make an arithmetic mistake. It answered a slightly different question.

::: play
- Move x and y to place the point anywhere. The arrow runs from the origin to it.
- The strip shows two angles: $\tan^{-1}(y/x)$, what a calculator gives, and atan2, the angle the arrow actually makes.
- Visit all four quadrants. In which ones do the two angles agree? Where they disagree, by how much?
:::
:::

```sim plot
view: complex
range: 6
slider: x = -5..5 step 0.1 = -3
slider: y = -5..5 step 0.1 = 4
curve: abs(x + j*y)*exp(j*u) | over: u = 0..arg(x + j*y) | dashed | color: ch2
arrow: x + j*y | label: (x, y)
readout: tan⁻¹(y/x) = atan(y/x)*180/pi °
readout: atan2 = arg(x + j*y)*180/pi °
readout: distance = abs(x + j*y)
```

::: predict
## Where does the point at $x = -3$, $y = 4$ really point?

- [ ] −53.1°
- [ ] 53.1°
- [x] 126.9°
- [ ] 233.1°

::: reveal
126.9°. The ratio $y/x = -4/3$ is the same for $(-3, 4)$ and for $(3, -4)$, two points on opposite sides of the origin, and $\tan^{-1}$ only sees the ratio, so it has to pick one: it always answers between −90° and 90°, on the right-hand side. For a point on the left you add 180°: −53.1° + 180° = 126.9°. atan2 does that for you, because it looks at the signs of x and y separately.
:::
:::

::: nameit
## Inverse functions undo, within a range

The **inverse** of a function $f$, written $f^{-1}$, undoes it: if $f(a) = b$, then $f^{-1}(b) = a$. You already know several pairs:

| $f$ | $f^{-1}$ |
|---|---|
| $10^{x}$ | $\log_{10} x$ |
| $e^{x}$ | $\ln x$ |
| $x^2$, for $x \ge 0$ | $\sqrt{x}$ |
| $2x - 6$ | $(x + 6)/2$ |

To find a formula for $f^{-1}$, write $y = f(x)$ and solve for $x$. The graph of $f^{-1}$ is the graph of $f$ reflected in the line $y = x$, since every point $(a, b)$ becomes $(b, a)$.

An inverse only exists where $f$ never repeats an output: if two inputs give the same output, the inverse can't know which one to return. $x^2$ gives 9 for both 3 and −3, so $\sqrt9$ is defined to return the positive one. The inverse trig functions make the same kind of choice, a **principal range**:

| Function | Returns |
|---|---|
| $\sin^{-1} x$ (arcsin) | −90° to 90° |
| $\cos^{-1} x$ (arccos) | 0° to 180° |
| $\tan^{-1} x$ (arctan) | between −90° and 90° |

So $\sin^{-1}(0.5) = 30°$, though 150° has the same sine. When you know the angle should be somewhere else, you fix it yourself: $180° - 30° = 150°$.

**atan2.** For the angle of a point $(x, y)$, there's a function that uses both coordinates and always gets the quadrant right:

- if $x > 0$, $\text{atan2}(y, x) = \tan^{-1}(y/x)$;
- if $x < 0$, add 180° to that (or subtract 180°, to stay between −180° and 180°);
- if $x = 0$, the angle is 90° or −90°, by the sign of $y$.

Every programming language has it, usually as `atan2(y, x)` with $y$ first. You'll use it constantly: the phase of an AC voltage, the angle of a complex number and the direction of a field all come from atan2 of two components.
:::

::: example
## Find the inverse of $f(x) = 3x - 4$ and check it.

::: solution
1. **Solve for x.** Write $y = 3x - 4$ and solve:
   $$x = \frac{y + 4}{3}$$
2. **Write the inverse.**
   $$f^{-1}(x) = \frac{x + 4}{3}$$
3. **Check.**
   $$\begin{aligned} f^{-1}(5) &= 3 \\ f(3) &= 9 - 4 \\ &= 5 \end{aligned}$$
   Each undoes the other.

**Answer:** $f^{-1}(x) = \dfrac{x + 4}{3}$.
:::
:::

::: example
## What angle does the point $(-3, -3)$ make with the positive $x$ axis?

::: solution
1. **Plain inverse tangent.**
   $$\begin{aligned} \tan^{-1}(-3/-3) &= \tan^{-1}(1) \\ &= 45° \end{aligned}$$
   which points up and to the right.
2. **Find the quadrant.** The point is down and to the left, in the third quadrant.
3. **Fix it with atan2.**
   $$45° - 180° = -135°$$
   or 225°, measured the long way round.

**Answer:** the point makes an angle of −135° with the positive $x$ axis (or 225°, measured the long way round).
:::
:::

::: example
## A voltage gain in decibels is $G = 20\log_{10} r$, where $r$ is the voltage ratio. Write the inverse, and find the ratio for 26 dB and the decibels for a ratio of 50.

::: solution
1. **Invert.** Divide by 20 and undo the logarithm with a power of ten:
   $$r = 10^{G/20}$$
2. **Ratio for 26 dB.**
   $$\begin{aligned} r &= 10^{1.3} \\ &= 20.0 \end{aligned}$$
3. **Decibels for a ratio of 50.**
   $$\begin{aligned} G &= 20\log_{10} 50 \\ &= 34.0\ \text{dB} \end{aligned}$$
4. **Check.** A function and its inverse undo each other: $10^{34.0/20}$ gives back 50.

**Answer:** the inverse is $r = 10^{G/20}$. 26 dB is a ratio of 20.0, and a ratio of 50 is 34.0 dB.
:::
:::

::: check
given: x = -5 | -4 | -3 | -2 | -1; y = -5 | -4 | -3 | -2 | -1 | 1 | 2 | 3 | 4 | 5
answer: arg(x + j*y)*180/pi
unit: °
tolerance: 0.5%

## What angle does the point $(\given{x}, \given{y})$ make with the positive horizontal axis? Give it between −180° and 180°.

::: right
Right. The point is on the left, so $\tan^{-1}(y/x)$ is half a turn off, and atan2 corrects it: {answer}.
:::

::: misses
- [atan(y/x)*180/pi] That's what $\tan^{-1}(y/x)$ gives, which always points to the right-hand side. This point has a negative x, so add or subtract 180°.
- [atan(y/x)*180/pi + 360] Check which quadrant the point is in, then adjust by 180°.
- [any] Not yet. Find $\tan^{-1}(y/x)$, then correct it by 180° since x is negative.
:::
:::

::: check
given: m = 2..6 int; k = 1..9 int; y = 10..40 int
answer: (y - k)/m
tolerance: 0.5%

## $f(x) = \given{m}x + \given{k}$. What is $f^{-1}(\given{y})$?

::: right
Right. $f^{-1}(\given{y})$ is the input that gives {y}: subtract {k} and divide by {m}.
:::

::: misses
- [m*y + k] That's $f(\given{y})$. The inverse goes the other way: which input gives {y}?
- [1/(m*y + k)] $f^{-1}$ means the inverse function, not one over $f$.
- [any] Not yet. Solve $\given{m}x + \given{k} = \given{y}$ for x.
:::
:::

::: check
kind: expression
answer: (x + 6)/2
vars: x = -5..5

## Find the inverse of $f(x) = 2x - 6$, as a formula in $x$.

::: right
Right. Write $y = 2x - 6$, solve for $x = (y + 6)/2$, then rename the variable.
:::

::: misses
- [x/2 + 6] Undo the steps in reverse order: add 6 first, then divide by 2.
- [1/(2*x - 6)] That's one over $f$. The inverse undoes $f$.
- [2*x + 6] Undo the multiplying by 2 by dividing.
- [any] Not yet. Set $y = 2x - 6$ and solve for $x$.
:::
:::

::: check
given: s = 0.2 | 0.3 | 0.4 | 0.6 | 0.7 | 0.8
answer: 180 - asin(s)*180/pi
unit: °
tolerance: 0.5%

## An angle between 90° and 180° has a sine of {s}. What is the angle?

::: right
Right. $\sin^{-1}$ gives the angle in the first quadrant, and its mirror image on the left, 180° minus it, has the same sine: {answer}.
:::

::: misses
- [asin(s)*180/pi] That's the calculator's answer, between −90° and 90°. The angle you want has the same sine but sits on the left side.
- [180 + asin(s)*180/pi] That's in the third quadrant, where sines are negative. Reflect across the vertical axis instead: 180° minus the calculator's angle.
- [any] Not yet. Find $\sin^{-1}$ of {s}, then use $\sin(180° - \theta) = \sin\theta$.
:::
:::

::: check
kind: choice

## Which of these has an inverse over all the real numbers, with no range restriction?

- [ ] $x^2$
- [x] $e^{x}$
- [ ] $\sin x$
- [ ] $|x|$

::: right
Right. $e^x$ always increases, so it never repeats an output, and $\ln x$ undoes it.
:::

::: misses
- [1] $x^2$ gives the same output for $x$ and $-x$, so it needs restricting to $x \ge 0$.
- [3] $\sin x$ repeats every 360°, so it needs a principal range.
- [4] $|x|$ gives the same output for $x$ and $-x$.
:::
:::

::: check
kind: parts
given: a = 0.4 | 0.5; b = 0.01 | 0.02; v = 1.2 | 1.8; G = 6 | 20 | 34; r = 3 | 50 | 200

## Two functions and their inverses.

::: part
answer: (v - a)/b
unit: °C
tolerance: 0.5%

### A sensor puts out $V = \given{a} + \given{b}T$ volts at $T$ °C. What temperature gives {v} V?

::: misses
- [(v + a)/b] Subtract the offset before dividing.
- [any] Solve for $T$: $T = (V - \given{a})/\given{b}$.
:::
:::

::: part
answer: 10^(G/20)
tolerance: 0.5%

### Gain in decibels is $G = 20\log_{10} r$. What voltage ratio is {G} dB?

::: misses
- [10^(G/10)] For a voltage ratio the decibels are $20\log_{10}$, so divide by 20.
- [any] The inverse is $r = 10^{G/20}$.
:::
:::

::: part
answer: 20*log(r)
unit: dB
tolerance: 0.5%

### How many decibels is a voltage ratio of {r}?

::: misses
- [10*log(r)] For a voltage ratio it's $20\log_{10}$.
- [any] $G = 20\log_{10} r$.
:::
:::
:::

::: check
kind: derivation
vars: a = 1..3, b = 0.5..2, c = 0.5..2, d = 4..6, y = -0.5..0.1

## Derive the inverse of $f(x) = \dfrac{ax + b}{cx + d}$.

::: step
answer: b - d*y

### Set $y = f(x)$, multiply both sides by $cx + d$, and collect the $x$ terms on one side: $x(cy - a) = \ ?$
:::

::: step
answer: (b - d*y)/(c*y - a)

### Solve for $x$. That's $f^{-1}(y)$.
:::

::: right
Right. $f^{-1}(y) = \frac{b - dy}{cy - a}$, which fails only at $y = a/c$, the value $f$ approaches but never reaches.
:::
:::

::: explain
## Why can't $\tan^{-1}(y/x)$ alone tell you the angle of a point, and what extra information fixes it?

::: model
Dividing y by x throws away the signs as a pair: $(3, 4)$ and $(-3, -4)$ give the same ratio, though they point in opposite directions. $\tan^{-1}$ only sees the ratio, so it returns the angle on the right-hand side, between −90° and 90°. Knowing the sign of x fixes it: if x is negative, the point is on the left and the true angle is 180° away. atan2 takes x and y separately so it can do this.
:::
:::

::: practice

::: check
given: x = -5..5 int; y = -5 | -4 | -3 | -2 | -1 | 1 | 2 | 3 | 4 | 5
answer: arg(x + j*y)*180/pi
unit: °
tolerance: 0.5%

## Give the angle of the point $(\given{x}, \given{y})$, between −180° and 180°.

::: misses
- [any] Find $\tan^{-1}(y/x)$, then adjust by 180° if x is negative. On the vertical axis, it's ±90°.
:::
:::

::: check
given: v = 2..50 sig 2
answer: ln(v)
tolerance: 0.5%

## $e^{x} = \given{v}$. What is $x$?

::: misses
- [log10(v)] The inverse of $e^x$ is the natural log, ln.
- [any] Take the natural log of both sides.
:::
:::

::: check
given: v = 2..5000 sig 2
answer: log10(v)
tolerance: 0.5%

## $10^{x} = \given{v}$. What is $x$?

::: misses
- [ln(v)] ln undoes $e^x$. The inverse of $10^x$ is $\log_{10}$.
- [any] Take $\log_{10}$ of both sides.
:::
:::

::: check
given: c = -0.9 | -0.5 | -0.2 | 0.1 | 0.3 | 0.7 | 0.95
answer: acos(c)*180/pi
unit: °
tolerance: 0.5%

## What does $\cos^{-1}(\given{c})$ return, in degrees?

::: misses
- [-acos(c)*180/pi] $\cos^{-1}$ returns an angle between 0° and 180°.
- [any] Use $\cos^{-1}$ on a calculator in degree mode; it answers between 0° and 180°.
:::
:::

::: check
given: a = 2..5 int; b = 1..9 int
answer: (x - b)/a
vars: x = -5..5
kind: expression

## Find the inverse of $f(x) = \given{a}x + \given{b}$.

::: misses
- [x/a - b] Undo in reverse order: subtract {b} first, then divide by {a}.
- [any] Write $y = \given{a}x + \given{b}$ and solve for x.
:::
:::
:::
