Volume 1: Foundations to Precalculus · Part IV: Functions, Trigonometry, and Precalculus
Chapter 13. Trigonometry
Trigonometry began as a way to measure what you can’t reach: the height of a tree, the width of a river, the distance to a ship or a star. It works because, in a right triangle, the ratios of the sides depend only on the angles. Extend those ratios from triangles to the full circle and they become the sine and cosine functions, the basic description of anything that rotates, oscillates or repeats. That covers pendulums and springs, sound and radio, and every rotation in a game engine. This chapter takes that whole path, from a measuring tape to atan2 and a first look at Fourier analysis.
13.1. Measuring what you can’t reach: right-triangle trigonometry
13.1.1. The problem
You want the height of a tree. You can’t climb it, but you can walk away from its base. You pace out 30 m along flat ground, then use a phone’s inclinometer app to measure the angle from the ground up to the treetop: 35°. That’s all you have, one distance and one angle. Is it enough?
Yes, it is. Once you know the angle and the ground distance, only one right triangle fits, so the height is determined. You just need a way to compute it.
13.1.2. Intuition: shape is fixed by angles
Think back to similar triangles (Chapter 9). Two triangles with the same angles have the same shape, and one is a scaled copy of the other. Scaling multiplies every side by the same factor, so the ratio of any two sides doesn’t change.
So for a right triangle with a 35° angle, the ratio \(\text{height}/\text{ground}\) is the same whether the triangle is drawn on a napkin or stretches across a valley. Measure it once on any 35° right triangle (the napkin will do) and you can use it for every 35° right triangle. People have tabulated these ratios for every angle, and now a calculator does it for you.
13.1.3. Vocabulary and notation
|
Opposite, adjacent, hypotenuse
In a right triangle, pick one of the two acute angles and call it \(\theta\) (the Greek letter theta, the traditional name for an angle).
"Opposite" and "adjacent" depend on which angle you are looking from. Swap to the other acute angle and they swap too. |
|
Sine, cosine, tangent
For an acute angle \(\theta\) in a right triangle:
\[\sin\theta = \frac{\text{opposite}}{\text{hypotenuse}}, \qquad
\cos\theta = \frac{\text{adjacent}}{\text{hypotenuse}}, \qquad
\tan\theta = \frac{\text{opposite}}{\text{adjacent}}.\]
The mnemonic is SOH-CAH-TOA: Sine = Opposite/Hypotenuse, Cosine = Adjacent/Hypotenuse, Tangent = Opposite/Adjacent. Dividing the first by the second gives \(\tan\theta = \dfrac{\sin\theta}{\cos\theta}\). Notation: \(\sin\theta\) means "sine of \(\theta\)". The brackets are optional for a single symbol but needed for anything longer: \(\sin(2\theta + 1)\). \(\sin^2\theta\) means \((\sin\theta)^2\), not \(\sin(\sin\theta)\). |
|
Reciprocal ratios
Three more names you’ll meet in older texts and in calculus:
\[\csc\theta = \frac{1}{\sin\theta}\ (\text{cosecant}), \qquad
\sec\theta = \frac{1}{\cos\theta}\ (\text{secant}), \qquad
\cot\theta = \frac{1}{\tan\theta}\ (\text{cotangent}).\]
Note the pairing: *co*secant goes with sine, and secant goes with *co*sine. They add no new information, only convenient shorthand. |
13.1.4. Worked example: the tree
The 30 m ground distance is adjacent to the 35° angle, and the height we want is opposite it. The ratio linking opposite and adjacent is tangent:
One refinement: your eye (or phone) isn’t at ground level. If it is 1.5 m up, the triangle starts at that height, and the tree is about \(21.0 + 1.5 = 22.5\) m tall. Real surveying is full of corrections like this.
13.1.5. The method
Input: a right triangle with two known quantities besides the right angle, at least one of which is a side.
Steps:
-
Sketch the triangle. Mark the right angle, the known angle \(\theta\), and label the sides opposite, adjacent and hypotenuse relative to \(\theta\).
-
Identify the side you know and the side you want.
-
Choose the ratio that contains exactly those two: both legs → tan; a leg and the hypotenuse → sin (if the leg is opposite) or cos (if adjacent).
-
Write the equation and solve for the unknown (multiply if it’s on top, divide if it’s underneath).
-
If you know two sides and want an angle, form the ratio and apply the inverse function (below).
-
For the remaining pieces: the third side comes from Pythagoras (\(a^2 + b^2 = c^2\)), and the other acute angle is \(90^\circ - \theta\).
-
Check: the hypotenuse must be the longest side, and the larger angle must face the larger side.
Output: all three sides and all three angles.
13.1.6. Worked example: a ladder
A 6 m ladder leans against a wall at 70° to the ground. How high up the wall does it reach, and how far is its foot from the wall?
The ladder is the hypotenuse. The height on the wall is opposite the 70° angle, and the distance from the wall is adjacent to it.
13.1.7. Inverse trigonometric functions
Now the reverse problem. A wheelchair ramp rises 1 m over a horizontal run of 12 m. What angle does it make with the ground?
Here we know \(\tan\theta = 1/12\) and want \(\theta\). The function that answers "which angle has this tangent?" is the inverse tangent, written \(\tan^{-1}\) or \(\arctan\):
|
Inverse trig functions
\(\sin^{-1} x\) (also \(\arcsin x\)), \(\cos^{-1} x\) (\(\arccos x\)) and \(\tan^{-1} x\) (\(\arctan x\)) return the angle whose sine, cosine or tangent is \(x\).
\[\theta = \sin^{-1} x \quad\Longleftrightarrow\quad \sin\theta = x \ \text{ and } \theta \text{ is in the principal range}.\]
The principal ranges are \(-90^\circ \le \sin^{-1} x \le 90^\circ\), \(0^\circ \le \cos^{-1} x \le 180^\circ\) and \(-90^\circ < \tan^{-1} x < 90^\circ\). There are infinitely many angles with a given sine, so the inverse function has to pick one (see Section 13.3). \(\sin^{-1} x\) is not \(1/\sin x\). The \(-1\) here means "inverse function", as in \(f^{-1}\) (Chapter 11), not "reciprocal". This is a notorious notational clash, and it’s why many people prefer writing arcsin. |
|
Calculator mode and impossible ratios
|
13.1.8. Special angles you can compute exactly
Three angles come up so often that their exact ratios are worth deriving once.
45°. Take a square with side 1 and cut it along a diagonal. Each half is a right isosceles triangle with legs 1, 1 and hypotenuse \(\sqrt{1^2 + 1^2} = \sqrt2\):
30° and 60°. Take an equilateral triangle with side 2 (all angles 60°) and drop a perpendicular from the top vertex. It bisects the base, giving a right triangle with hypotenuse 2, short leg 1, angles 30° and 60°, and long leg \(\sqrt{2^2 - 1^2} = \sqrt3\):
| \(\theta\) | \(\sin\theta\) | \(\cos\theta\) | \(\tan\theta\) |
|---|---|---|---|
0° |
0 |
1 |
0 |
30° |
\(\tfrac12\) |
\(\tfrac{\sqrt3}{2}\) |
\(\tfrac{1}{\sqrt3} = \tfrac{\sqrt3}{3}\) |
45° |
\(\tfrac{\sqrt2}{2}\) |
\(\tfrac{\sqrt2}{2}\) |
1 |
60° |
\(\tfrac{\sqrt3}{2}\) |
\(\tfrac12\) |
\(\sqrt3\) |
90° |
1 |
0 |
undefined |
A pattern to remember: down the sine column the values are \(\tfrac{\sqrt0}{2}, \tfrac{\sqrt1}{2}, \tfrac{\sqrt2}{2}, \tfrac{\sqrt3}{2}, \tfrac{\sqrt4}{2}\), and cosine is the same list read upward. Cosine is the sine of the *co*mplementary angle: \(\cos\theta = \sin(90^\circ - \theta)\). That’s where the "co" in cosine comes from.
13.1.9. Where you’d use this
-
Construction and DIY: heights of buildings, roof pitch, the grade of a road. A 6% grade means \(\tan\theta = 0.06\), so \(\theta \approx 3.43^\circ\). The US ADA maximum ramp slope of 1:12 is the 4.76° we just computed.
-
Shopping for a screen: a "65-inch" 16:9 TV is measured on the diagonal. The diagonal makes angle \(\theta = \tan^{-1}(9/16) \approx 29.36^\circ\) with the bottom edge, so the width is \(65\cos\theta \approx 56.7\) inches and the height \(65\sin\theta \approx 31.9\) inches. That’s what tells you whether it fits the alcove.
-
Mechanics: a 5 kg box rests on a ramp at 30°. Its weight, \(5 \times 9.81 = 49.05\) N, splits into \(49.05\sin 30^\circ \approx 24.5\) N pulling it down the slope and \(49.05\cos 30^\circ \approx 42.5\) N pressing into the surface. Friction has to supply the 24.5 N or the box slides. We develop components properly in Chapter 26.
-
Computing: a drone camera with an 84° field of view, hovering 120 m up and pointing straight down, sees a strip of ground \(2 \times 120 \tan 42^\circ \approx 216\) m wide (half the angle, half the width, then double). Mapping software uses exactly this to plan how far apart to space the photos. Game engines use the same formula to decide what the camera can see.
|
Why "sine"?
The word "sine" comes from a chain of mistranslations. Indian astronomers (Aryabhata, around 500 CE) used jyā-ardha, "half-chord", shortened to jyā. Arabic scholars transliterated it as jība, written without vowels as jb. Later Latin translators read this as jaib, "fold" or "bosom", and translated it as the Latin sinus, which means the same thing. "Tangent" (Latin tangens, "touching") and "secant" (secans, "cutting") are named after lines that touch or cut a circle, as Section 13.3 shows. |
13.2. Radians: measuring angles by arc length
13.2.1. The problem
Why does a full turn have 360 degrees? The Babylonians liked base 60, and 360 is close to the number of days in a year and divides evenly by many numbers. Those are cultural reasons, not mathematical ones. If you had to pick a unit for angles that came from the geometry of the circle itself, what would you choose?
13.2.2. Intuition
An angle at the centre of a circle cuts off an arc. Larger angles cut off longer arcs, in proportion. On a circle of radius 1, the arc length could be the measure of the angle. On a circle of radius \(r\) everything scales by \(r\), so the natural measure is
That’s the radian. One radian is the angle whose arc is exactly one radius long. Try it: See one radian.
13.2.3. Converting
A full circle has circumference \(2\pi r\), so a full turn is \(2\pi r / r = 2\pi\) radians. Therefore
|
Radian
An angle of \(\theta\) radians at the centre of a circle of radius \(r\) cuts off an arc of length \(s = r\theta\). Radians are a ratio of two lengths, so they have no physical unit, and "rad" is often left out. If no unit is written, assume radians. Converting: multiply degrees by \(\dfrac{\pi}{180}\) to get radians, and multiply radians by \(\dfrac{180}{\pi}\) to get degrees. Common values: \(30^\circ = \tfrac{\pi}{6}\), \(45^\circ = \tfrac{\pi}{4}\), \(60^\circ = \tfrac{\pi}{3}\), \(90^\circ = \tfrac{\pi}{2}\), \(180^\circ = \pi\), \(270^\circ = \tfrac{3\pi}{2}\), \(360^\circ = 2\pi\). |
13.2.4. Worked examples
Convert 135° to radians: \(135 \times \dfrac{\pi}{180} = \dfrac{135\pi}{180} = \dfrac{3\pi}{4} \approx 2.356\). Try it: Convert 135° interactively.
Convert \(\tfrac{5\pi}{6}\) to degrees: \(\dfrac{5\pi}{6} \times \dfrac{180}{\pi} = \dfrac{5 \times 180}{6} = 150^\circ\).
Arc length. A pizza with radius 18 cm is cut into 8 equal slices. How long is the crust edge of one slice? Try it: Arc length of the pizza slice.
Sector area. A sector with angle \(\theta\) is the fraction \(\theta / 2\pi\) of the whole disc, so its area is \(\dfrac{\theta}{2\pi}\cdot \pi r^2 = \tfrac12 r^2\theta\). For the pizza slice: \(\tfrac12 \times 18^2 \times \tfrac{\pi}{4} = 40.5\pi \approx 127.2\ \text{cm}^2\).
The nautical mile. The Earth’s radius is about 6,371 km. One minute of arc (\(1/60\) of a degree) along a great circle is
which is (to within rounding) the nautical mile, 1.852 km. Sea and air navigation still use it for this reason. One degree of latitude is about 111 km.
13.2.5. Why radians are natural
With degrees, \(s = r\theta\) would need a conversion factor: \(s = \tfrac{\pi}{180} r\theta\). Radians remove it. The real payoff comes in calculus. Look at small angles:
| \(\theta\) (radians) | \(\sin\theta\) | \(\sin\theta / \theta\) |
|---|---|---|
0.5 |
0.479426 |
0.958851 |
0.1 |
0.099833 |
0.998334 |
0.01 |
0.0099998 |
0.999983 |
Try it: Watch sin θ/θ approach 1.
For small \(\theta\) in radians, \(\sin\theta \approx \theta\). In Chapter 16 this becomes \(\lim_{\theta\to 0} \frac{\sin\theta}{\theta} = 1\), and in Chapter 17 it gives the clean rule "the derivative of \(\sin x\) is \(\cos x\)". In degrees the rule would read "the derivative of \(\sin x\) is \(\tfrac{\pi}{180}\cos x\)", and that factor would follow you through every formula in physics. Radians are the unit in which the circle’s own geometry needs no conversion constant.
The small-angle approximation \(\sin\theta \approx \theta\) is also what makes the simple pendulum formula work (Section 13.8).
13.2.6. Where you’d use this
Radians are the natural unit whenever something turns and you care how far a point on it travels, because the link is simply \(s = r\theta\), or per second, \(v = r\omega\).
-
Cycling: a road bike’s wheel has radius about 0.35 m. At 25 km/h (\(6.94\) m/s) the wheel turns at \(\omega = v/r = 6.94/0.35 \approx 19.8\) rad/s, which is \(19.8 \times 60 / 2\pi \approx 189\) revolutions per minute. A bike computer does this calculation in reverse: it counts wheel revolutions and multiplies by \(2\pi r\).
-
Engineering: a hard disk spinning at 7,200 rpm turns at \(7200 \times 2\pi / 60 \approx 754\) rad/s. A track 4.5 cm from the centre passes under the head at \(0.045 \times 754 \approx 33.9\) m/s, more than 120 km/h. The outer tracks move faster than the inner ones, which is why they read faster.
-
Robotics: a robot’s wheel encoder gives 1,024 ticks per revolution and the wheel has radius 5 cm. Each tick is \(2\pi/1024\) rad, so the robot moves \(0.05 \times 2\pi/1024 \approx 0.307\) mm per tick. Counting ticks is how a robot vacuum knows how far it has gone (odometry).
|
Units, again
Every mainstream programming language’s |
13.3. The unit circle: trigonometry for every angle
13.3.1. The problem
SOH-CAH-TOA needs a right triangle, so it only works for angles between 0° and 90°. But a wheel turns through 120°, 270° and 1,000°, and a robot arm can rotate by \(-45^\circ\). What is \(\sin 150^\circ\)? What is \(\cos(-30^\circ)\)? We need definitions that work for any angle and agree with the triangle definitions where those apply.
13.3.2. Intuition: follow a point round a circle
Draw a circle of radius 1 centred at the origin, the unit circle. Start at the point \((1, 0)\) and move anticlockwise around the circle through an angle \(\theta\). Call where you end up \(P\).
For an acute angle, drop a perpendicular from \(P\) to the \(x\)-axis. That gives a right triangle with hypotenuse 1, so the adjacent side (the \(x\)-coordinate of \(P\)) is \(\cos\theta\) and the opposite side (the \(y\)-coordinate) is \(\sin\theta\). So for acute angles,
Now turn this around and make it the definition for every angle.
|
Unit-circle definition of sine and cosine
Rotate the point \((1,0)\) anticlockwise about the origin by angle \(\theta\) (clockwise if \(\theta\) is negative). The point lands at \(P\). Then
\[\cos\theta = x\text{-coordinate of } P, \qquad \sin\theta = y\text{-coordinate of } P, \qquad \tan\theta = \frac{\sin\theta}{\cos\theta} = \text{slope of } OP.\]
For an angle \(\theta\) in standard position, the initial side is the positive \(x\)-axis and the terminal side is the ray \(OP\). The four quadrants are numbered I to IV anticlockwise, starting from the top right. |
13.3.3. What the definition gives you immediately
Signs by quadrant. The signs of \(x\) and \(y\) in each quadrant give:
| Quadrant | Angles | sin | cos | tan |
|---|---|---|---|---|
I |
0° to 90° |
+ |
+ |
+ |
II |
90° to 180° |
+ |
− |
− |
III |
180° to 270° |
− |
− |
+ |
IV |
270° to 360° |
− |
+ |
− |
(A traditional mnemonic, anticlockwise from quadrant I: "*A*ll *S*tudents *T*ake *C*alculus". All three are positive in I, Sine in II, Tangent in III, Cosine in IV.)
Bounds. \(P\) is on a circle of radius 1, so \(-1 \le \sin\theta \le 1\) and \(-1 \le \cos\theta \le 1\).
Periodicity. Adding a full turn brings you back to the same point:
Sine and cosine are periodic with period \(2\pi\). Tangent repeats every \(\pi\), because \(P\) and the opposite point \(-P\) give the same slope.
Symmetry. Reflecting in the \(x\)-axis turns angle \(\theta\) into \(-\theta\). That flips \(y\) and leaves \(x\) unchanged:
13.3.4. Reference angles: the method
Input: an angle \(\theta\) (degrees or radians) and a function (sin, cos or tan).
Steps:
-
Add or subtract multiples of 360° (\(2\pi\)) until the angle is between 0° and 360°.
-
Find the quadrant of the terminal side.
-
Find the reference angle \(\alpha\): the acute angle between the terminal side and the \(x\)-axis.
-
Quadrant I: \(\alpha = \theta\). II: \(\alpha = 180^\circ - \theta\). III: \(\alpha = \theta - 180^\circ\). IV: \(\alpha = 360^\circ - \theta\).
-
-
Evaluate the function at \(\alpha\) (special-angle table or calculator).
-
Attach the sign that function has in that quadrant.
Output: the exact or decimal value.
13.3.5. Worked examples
\(\sin 150^\circ\). Quadrant II, reference angle \(180^\circ - 150^\circ = 30^\circ\), sine is positive in II: \(\sin 150^\circ = +\sin 30^\circ = \tfrac12\). Try it: Evaluate sin 150° step by step.
\(\cos 240^\circ\). Quadrant III, reference angle \(240^\circ - 180^\circ = 60^\circ\), cosine is negative in III: \(\cos 240^\circ = -\cos 60^\circ = -\tfrac12\). Try it: Evaluate cos 240° step by step.
\(\tan\!\left(\tfrac{11\pi}{6}\right)\). \(\tfrac{11\pi}{6} = 330^\circ\): quadrant IV, reference angle 30°, tangent is negative in IV: \(\tan 330^\circ = -\tan 30^\circ = -\tfrac{\sqrt3}{3}\). Try it: Evaluate tan(11π/6) step by step.
\(\sin 765^\circ\). \(765 - 2 \times 360 = 45\), so \(\sin 765^\circ = \sin 45^\circ = \tfrac{\sqrt2}{2}\). Try it: Evaluate sin 765° step by step.
13.3.6. Solving trig equations: many answers
Because the functions repeat, an equation like \(\sin x = \tfrac12\) has infinitely many solutions. Your calculator’s \(\sin^{-1}\) returns just one of them.
Solve \(\sin x = \tfrac12\) for \(0 \le x < 2\pi\): Try it: Find every solution of sin x = ½.
The general solution is \(x = \tfrac{\pi}{6} + 2\pi k\) or \(x = \tfrac{5\pi}{6} + 2\pi k\) for any integer \(k\).
|
The inverse gives one answer, not all of them
\(\sin^{-1}\), \(\cos^{-1}\) and \(\tan^{-1}\) return a single principal value. If the problem has a range, such as "all angles in a full turn", you have to find the others yourself using symmetry: for sine, \(\pi - x\); for cosine, \(-x\) (or \(2\pi - x\)); for tangent, \(x + \pi\). Forgetting the second solution is the commonest error in trig equations. In code it’s the reason |
|
Why "tangent" and "secant"?
Draw the vertical line \(x = 1\), which touches (is tangent to) the unit circle at \((1,0)\). Extend the ray \(OP\) until it meets that line. The height where it meets is exactly \(\tan\theta\), and the length of the extended ray from \(O\) (a line that cuts the circle, a secant) is exactly \(\sec\theta\). The names describe a picture, and the picture also explains why \(\tan 90^\circ\) is undefined: at 90° the ray runs parallel to the tangent line and never meets it. |
|
Spin the unit circle
Drag a point around the unit circle. Show its coordinates \((\cos\theta, \sin\theta)\) live, colour the quadrant, and draw the reference angle. Next to it, trace \(\sin\theta\) and \(\cos\theta\) as graphs against \(\theta\) as the point moves, so the reader sees the circle "unwrap" into waves. Add a toggle between degrees and radians. Try it: Unit circle unwrapped · Spin the unit circle |
13.4. Graphs of sine, cosine and tangent
13.4.1. From circle to wave
Plot the \(y\)-coordinate of the moving point against the angle it has turned through, and you get the sine wave. Plot the \(x\)-coordinate and you get the cosine wave, which is the same shape shifted left by a quarter turn: \(\cos x = \sin(x + \tfrac{\pi}{2})\).
Key features:
-
\(\sin x\): zeros at \(x = k\pi\), maximum 1 at \(\tfrac{\pi}{2} + 2k\pi\), minimum −1 at \(\tfrac{3\pi}{2} + 2k\pi\). Domain all reals, range \(\lbrack -1, 1]\).
-
\(\cos x\): the same shape, starting at its maximum: \(\cos 0 = 1\).
-
\(\tan x\): zeros where \(\sin x = 0\). It is undefined and has vertical asymptotes where \(\cos x = 0\), at \(x = \tfrac{\pi}{2} + k\pi\). Range is all reals.
13.4.2. Amplitude, period, phase and midline
Real oscillations don’t swing exactly between −1 and 1 with period \(2\pi\). You get the general sinusoid by applying the function transformations from Chapter 11:
|
Sinusoid parameters
For \(y = A\sin(B(x - C)) + D\) (with \(A, B > 0\)):
|
|
Factor out \(B\) before reading the shift
In \(y = \sin(2x - \tfrac{\pi}{2})\) the phase shift is not \(\tfrac{\pi}{2}\). Factor first: \(\sin\big(2(x - \tfrac{\pi}{4})\big)\), so the shift is \(\tfrac{\pi}{4}\). Physicists often write \(\sin(\omega t + \varphi)\) and call \(\varphi\) (phi) the phase. The actual time shift is then \(-\varphi/\omega\). Check which convention a formula is using. |
Try it: Adjust amplitude, period and phase.
13.4.3. Worked example: modelling daylight
In a city at about 40° N, the longest day (around 21 June, day 172) has about 15 hours of daylight and the shortest (around 21 December, day 355) about 9 hours. Model daylight hours \(H\) against day of the year \(t\) with a sinusoid. Try it: Fit the daylight model.
So \(H(t) = 3\sin\!\big(\tfrac{2\pi}{365}(t - 80.75)\big) + 12\). Check day 172: the argument is \(\tfrac{2\pi}{365} \times 91.25 = \tfrac{\pi}{2}\), so \(H = 3 + 12 = 15\). Correct.
Input: the maximum and minimum values, the period, and the location of one maximum (or a rising midline crossing).
Steps:
-
Midline \(D = (\max + \min)/2\).
-
Amplitude \(A = (\max - \min)/2\).
-
\(B = 2\pi / \text{period}\).
-
Phase shift \(C\) = location of a rising midline crossing (for sine), or of a maximum (if you use cosine instead).
-
Write \(y = A\sin(B(x - C)) + D\) and check one known point.
Output: a sinusoidal model.
13.4.4. Where you’d use this
Anything that repeats on a schedule can be modelled, at least roughly, by a sinusoid, and the four parameters answer practical questions.
-
Boating (everyday): at a harbour, high water is 4.2 m at 03:00 and low water 0.6 m, and the tide repeats every 12.4 hours. Using cosine (which peaks at its shift), \(h(t) = 1.8\cos\!\big(\tfrac{2\pi}{12.4}(t - 3)\big) + 2.4\). A boat that needs 2.0 m of water to cross the harbour bar can cross while \(\cos(\ldots) \ge (2.0 - 2.4)/1.8 = -0.222\), that is, while \(|t - 3| \le \tfrac{12.4}{2\pi}\cos^{-1}(-0.222) \approx 3.54\) hours. The window is roughly 23:27 to 06:33. Sailors' "rule of twelfths" is a hand approximation to this sine curve. Try it: Find the tide window.
-
Electricity (engineering): UK mains is 230 V at 50 Hz, but 230 V is an average-power figure (see Section 13.5). The actual voltage is \(V(t) = 325\sin(2\pi \cdot 50\, t) = 325\sin(100\pi t)\) volts: amplitude 325 V, period \(1/50\) s = 20 ms. US mains is \(170\sin(120\pi t)\), which is 120 V at 60 Hz. Try it: Plot the mains voltage.
-
Computing: web traffic, CPU load and electricity demand all have a strong daily cycle. Capacity planners fit \(A\sin(\tfrac{2\pi}{24}(t - C)) + D\) to hourly data. \(D\) is the average load, \(A\) tells you how much headroom the peak needs, and \(C\) tells you when to schedule batch jobs, in the trough. Forecasting libraries such as Prophet model seasonality as a sum of sines and cosines of this kind.
13.5. Identities: Pythagorean, angle sum, double angle
13.5.1. The problem
In a physics problem you get \(\sin\theta = 0.6\) and need \(\cos\theta\). A graphics routine needs the sine of a sum of two rotations. A signal-processing derivation needs \(\sin^2 x\) written without the square. These all call for identities, equations that hold for every angle. They let you rewrite one trig expression as another.
|
Identity
An identity is an equation that holds for every value of the variable for which both sides are defined, such as \((a + b)^2 = a^2 + 2ab + b^2\). An ordinary equation such as \(\sin x = \tfrac12\) holds only for particular values. Some books write \(\equiv\) for identities. |
13.5.2. The Pythagorean identity, from the unit circle
The point \(P = (\cos\theta, \sin\theta)\) lies on the unit circle \(x^2 + y^2 = 1\) (Chapter 10). Substitute:
That’s Pythagoras' theorem for a right triangle with hypotenuse 1, and the unit-circle definition makes it hold for every angle, not just acute ones. Dividing through by \(\cos^2\theta\) or \(\sin^2\theta\) gives two variants:
Worked example. \(\sin\theta = 0.6\) and \(\theta\) is in quadrant II. Find \(\cos\theta\) and \(\tan\theta\).
13.5.3. Angle-sum formulas, derived geometrically
Is \(\sin(\alpha + \beta) = \sin\alpha + \sin\beta\)? Try \(\alpha = \beta = 90^\circ\): the left side is \(\sin 180^\circ = 0\), the right side is \(1 + 1 = 2\). No. So what is it?
The construction in the figure:
-
Draw the angle \(\alpha\) at \(O\) from the horizontal, then the angle \(\beta\) on top of it, giving a ray at angle \(\alpha + \beta\). Mark \(P\) on that ray with \(OP = 1\).
-
Drop a perpendicular from \(P\) onto the \(\alpha\)-ray, meeting it at \(Q\). Triangle \(OQP\) has a right angle at \(Q\) and hypotenuse 1, so \(OQ = \cos\beta\) and \(QP = \sin\beta\).
-
Drop a perpendicular from \(Q\) to the horizontal at \(R\). Triangle \(ORQ\) has hypotenuse \(\cos\beta\) and angle \(\alpha\), so \(OR = \cos\alpha\cos\beta\) and \(RQ = \sin\alpha\cos\beta\).
-
Draw a vertical line up from \(Q\) and a horizontal line across from \(P\). They meet at \(T\). In triangle \(QTP\), the angle at \(Q\) is \(\alpha\): \(QT\) is vertical, and \(QP\) is perpendicular to \(OQ\), so the angle \(QP\) makes with the vertical equals the angle \(OQ\) makes with the horizontal. With hypotenuse \(QP = \sin\beta\), we get \(QT = \cos\alpha\sin\beta\) and \(TP = \sin\alpha\sin\beta\).
Now read off \(P\)'s coordinates two ways:
The picture assumes acute angles with an acute sum, but the formulas hold for all angles. (The cleanest general proof uses rotation matrices, Chapter 27, or complex numbers, Chapter 15.) Replacing \(\beta\) with \(-\beta\) and using even/odd symmetry gives the difference formulas.
|
Angle-sum and difference formulas
\[\begin{aligned}
\sin(\alpha \pm \beta) &= \sin\alpha\cos\beta \pm \cos\alpha\sin\beta \\
\cos(\alpha \pm \beta) &= \cos\alpha\cos\beta \mp \sin\alpha\sin\beta \\
\tan(\alpha \pm \beta) &= \frac{\tan\alpha \pm \tan\beta}{1 \mp \tan\alpha\tan\beta}
\end{aligned}\]
\(\pm\) means "plus or minus" and \(\mp\) means "the opposite sign": take the top signs together or the bottom signs together. The cosine formula flips the sign. |
Worked example: an exact value for \(\sin 75^\circ\).
13.5.4. Double-angle formulas
Put \(\beta = \alpha\) in the sum formulas:
Using \(\sin^2\alpha + \cos^2\alpha = 1\) to remove one or the other gives two more forms of \(\cos 2\alpha\):
Rearranged, these are the power-reduction formulas that calculus uses all the time (Chapter 19):
Note what they say: the square of a wave at frequency \(f\) is a constant plus a wave at frequency \(2f\). That fact explains why the power of an AC signal pulses at twice the mains frequency.
13.5.5. Choosing an approach
-
Need one ratio given another? Use the Pythagorean identity, then fix the sign by quadrant.
-
Exact value at an awkward angle (15°, 75°, 105°)? Write it as a sum or difference of special angles.
-
Squared trig function to integrate or average? Use power reduction.
-
Proving an identity? Work on the more complicated side, convert everything to sin and cos, and look for \(\sin^2 + \cos^2\). Never "cross over" between the sides as if solving an equation, because that assumes what you’re trying to prove.
-
Just checking numerically? Plug in a random angle like 0.7 rad. That disproves a false identity instantly, though it can’t prove a true one.
|
Trig functions are not linear
\(\sin(\alpha + \beta) \ne \sin\alpha + \sin\beta\), \(\sin 2\alpha \ne 2\sin\alpha\), and \(\cos(x/2) \ne \tfrac12\cos x\). "sin" is a function name, not a number you can distribute or cancel. \(\dfrac{\sin x}{x}\) does not simplify to "sin", and \(\dfrac{\sin 2x}{\sin x}\) is \(2\cos x\), not 2. |
13.5.6. Where you’d use this
-
Electricity: why "230 volts" is not the peak. Power in a resistor is \(V^2/R\). With \(V = V_0\sin\omega t\), power reduction gives \(V^2 = V_0^2 \cdot \tfrac{1 - \cos 2\omega t}{2}\). The cosine term averages to zero over a cycle, so the average power is \(\tfrac{V_0^2}{2R}\): the same as a steady DC voltage of \(V_0/\sqrt2\). That is the RMS (root-mean-square) voltage printed on every appliance. For UK mains, \(325/\sqrt2 \approx 230\) V. The cosine term at \(2\omega\) is the power pulsing 100 times a second.
-
Music: tuning by beats. Add \(\sin(\alpha+\beta)\) and \(\sin(\alpha-\beta)\) using the angle-sum formulas and the \(\cos\alpha\sin\beta\) terms cancel: \(\sin(\alpha+\beta) + \sin(\alpha-\beta) = 2\sin\alpha\cos\beta\). Play a 440 Hz string against a 442 Hz tuning reference and you hear a 441 Hz tone whose loudness swells and fades as \(\cos(2\pi \cdot 1 \cdot t)\), peaking twice a second. Guitarists tune by turning the peg until those 2 Hz "beats" slow to nothing.
-
Computing: oscillators without calling
sin. The angle-sum formula gives \(\sin((n+1)\theta) = 2\cos\theta\,\sin(n\theta) - \sin((n-1)\theta)\). (Add the sum and difference formulas for \(\sin(n\theta \pm \theta)\).) An audio synthesiser or a graphics loop that needs \(\sin\) at evenly spaced angles computes \(2\cos\theta\) once, then gets each new value with one multiply and one subtract. The same recurrence generates rotation steps in old games and in DSP chips.
|
Ptolemy’s table of chords
Around 150 CE, Claudius Ptolemy built a table of chord lengths (equivalent to a sine table) in half-degree steps, accurate to about five decimal places. He used, in effect, the angle-sum and half-angle formulas, starting from the exactly known chords for 36°, 60°, 72° and 90° and working out the rest. His Almagest was the standard astronomy text for 1,400 years. Your CPU’s |
13.6. Any triangle: the laws of sines and cosines
13.6.1. The problem
A surveyor on one bank of a river marks two points \(A\) and \(B\), 500 m apart. A tree \(C\) stands on the far bank. From \(A\), the angle between \(B\) and the tree is 52°. From \(B\), the angle between \(A\) and the tree is 71°. How far is the tree from each point, and how wide is the river?
No right angle appears anywhere, so SOH-CAH-TOA doesn’t apply directly. We need tools for any triangle.
|
Labelling convention
In triangle \(ABC\), the angles at the vertices are \(A\), \(B\), \(C\), and the side opposite each angle gets the matching lower-case letter: side \(a\) faces angle \(A\), and so on. The angles always add to \(180^\circ\). |
13.6.2. The law of sines, derived
Drop the altitude \(h\) from \(C\) to side \(AB\). It splits the triangle into two right triangles, and in each one SOH gives:
Dropping an altitude from a different vertex brings \(c\) and \(C\) in the same way. The result is the law of sines:
Each side is proportional to the sine of the angle opposite it. (The common ratio has a meaning: it’s the diameter of the triangle’s circumscribed circle.)
The river, solved. \(C = 180^\circ - 52^\circ - 71^\circ = 57^\circ\), and \(c = AB = 500\). Try it: Solve the river triangle.
This is triangulation. From one carefully measured baseline and many angle measurements, surveyors mapped whole countries. The 18th–19th-century Great Trigonometrical Survey of India measured the height of Everest this way.
13.6.3. The law of cosines: Pythagoras, generalised
The law of sines needs at least one angle–opposite side pair. If you know two sides and the angle between them (or all three sides), you need the law of cosines instead.
Derivation. Use the triangle in the figure. Put \(A\) at the origin and \(B\) at \((c, 0)\). \(C\) is at distance \(b\) from \(A\) at angle \(A\), so \(C = (b\cos A,\ b\sin A)\). Then \(a\) is the distance from \(B\) to \(C\) (distance formula, Chapter 10):
|
Laws of sines and cosines
For any triangle:
\[\textbf{Sines:}\quad \frac{a}{\sin A} = \frac{b}{\sin B} = \frac{c}{\sin C}
\qquad\qquad
\textbf{Cosines:}\quad a^2 = b^2 + c^2 - 2bc\cos A\]
(and the same with the letters cycled: \(b^2 = a^2 + c^2 - 2ac\cos B\), \(c^2 = a^2 + b^2 - 2ab\cos C\)). When \(A = 90^\circ\), \(\cos A = 0\) and the law of cosines is Pythagoras. The term \(-2bc\cos A\) is the correction for a non-right angle. It is negative (side shorter) for acute \(A\) and positive (side longer) for obtuse \(A\). |
Worked example: three sides, find an angle. A triangle has sides 5, 7 and 8. Find the angle opposite the side of length 7. Try it: Solve the 5-7-8 triangle.
Worked example: navigation. A ship sails 20 km on a bearing of 040°, then turns and sails 30 km on a bearing of 110°. How far is it from port? Try it: Solve the navigation triangle.
Bearings are measured clockwise from north. The ship’s heading changes by \(110^\circ - 40^\circ = 70^\circ\), so the interior angle of the triangle at the turning point is \(180^\circ - 70^\circ = 110^\circ\).
The obtuse angle makes the distance longer than \(\sqrt{20^2 + 30^2} \approx 36.06\) km, as the sign rule predicts. (In Section 13.9 we solve the same problem with components and atan2, which also gives the bearing back to port.)
13.6.4. Choosing a method
| You know | Use |
|---|---|
Right triangle, any two pieces |
SOH-CAH-TOA and Pythagoras |
Two angles and any side (AAS, ASA) |
Law of sines (find the third angle from the 180° sum first) |
Two sides and the angle between them (SAS) |
Law of cosines for the third side |
Three sides (SSS) |
Law of cosines for an angle (start with the largest angle) |
Two sides and an angle not between them (SSA) |
Law of sines, but check for the ambiguous case |
|
The ambiguous case (SSA)
Given \(A = 30^\circ\), \(a = 6\), \(b = 10\): the law of sines gives \(\sin B = 10 \sin 30^\circ / 6 = 0.8333\). Your calculator says \(B \approx 56.44^\circ\), but \(180^\circ - 56.44^\circ = 123.56^\circ\) has the same sine. Both fit, since \(30 + 123.56 < 180\), so two different triangles match the data (with third sides about 11.98 and 5.34). Whenever you use \(\sin^{-1}\) to find an angle, ask whether its supplement also works. The law of cosines doesn’t have this problem, because \(\cos^{-1}\) covers 0° to 180° without ambiguity. That’s why SSS starts with cosines and finds the largest angle first. |
Try it: See both SSA solutions.
13.6.5. Where you’d use this
-
Sport: a baseball diamond is a 90 ft square, and the pitcher’s mound sits 60.5 ft from home plate on the diagonal towards second base. That diagonal makes 45° with the first-base line, so the throw from the mound to first base is \(\sqrt{60.5^2 + 90^2 - 2(60.5)(90)\cos 45^\circ} \approx 63.7\) ft. That’s SAS, so it’s a law-of-cosines problem.
-
Navigation and location: a hiker, a ship’s navigator or a search-and-rescue team takes compass bearings to two known landmarks and intersects them. That’s ASA, and the law of sines gives the distances. Mobile networks and Wi-Fi positioning do the same thing with signal angles (and, more often, with distances, which is trilateration).
-
Computing: a 3D mesh stores vertex positions, so every triangle’s three side lengths are known. A renderer or physics engine that needs the angle at a vertex (for smooth shading weights or for mesh-quality checks that flag long thin "sliver" triangles) gets it from SSS with the law of cosines. In robotics, a two-link arm with links of 30 cm and 25 cm that must reach a point 40 cm away needs an elbow angle \(C\) with \(40^2 = 30^2 + 25^2 - 2(30)(25)\cos C\), so \(\cos C = (900 + 625 - 1600)/1500 = -0.05\) and \(C \approx 92.9^\circ\). That’s inverse kinematics, and this is its simplest case.
|
Measuring the Earth
Around 240 BCE, Eratosthenes noticed that at noon on the summer solstice the Sun shone straight down a well in Syene, while in Alexandria, roughly 800 km north, a vertical stick cast a shadow showing the Sun was about 7.2° (one fiftieth of a circle) from vertical. Then \(s = r\theta\) gives the Earth’s circumference as about \(50 \times 800 = 40{,}000\) km, close to the true 40,075 km. That’s arc length and radians, more than 2,000 years before the word "radian". |
13.7. Polar coordinates
13.7.1. The problem
A radar screen reports a contact "5 km away at 30°". A robot vacuum knows its wheel has turned through some angle at some distance from its axle. Spirals, flower petals and orbits are awkward to describe as \(y = f(x)\). All of these are naturally given by how far and which direction, not by how far across and how far up.
13.7.2. Two descriptions of one point
|
Polar coordinates
A point’s polar coordinates \((r, \theta)\) are its distance \(r\) from the origin (the pole) and the angle \(\theta\) from the positive \(x\)-axis (the polar axis), measured anticlockwise.
\[\begin{aligned}
\text{Polar} \to \text{Cartesian:}\quad & x = r\cos\theta, \quad y = r\sin\theta \\
\text{Cartesian} \to \text{polar:}\quad & r = \sqrt{x^2 + y^2}, \quad \theta = \operatorname{atan2}(y, x)
\end{aligned}\]
The first line is the unit-circle definition scaled by \(r\). In the second, atan2 is the quadrant-aware inverse tangent (Section 13.9). Polar coordinates aren’t unique: \((r, \theta)\), \((r, \theta + 2\pi)\) and \((-r, \theta + \pi)\) all name the same point. |
13.7.3. Worked examples
Cartesian to polar: \((3, 2)\).
Cartesian to polar: \((-3, 2)\). \(r = \sqrt{13}\) again. \(\tan^{-1}(2/(-3)) = \tan^{-1}(-0.667) \approx -33.69^\circ\), which points into quadrant IV, the wrong quadrant. The point is in quadrant II, so add 180°: \(\theta \approx 146.31^\circ\). (atan2 does this correction for you.)
Polar to Cartesian: \(r = 4,\ \theta = \tfrac{5\pi}{6}\). \(x = 4\cos\tfrac{5\pi}{6} = 4 \times (-\tfrac{\sqrt3}{2}) = -2\sqrt3 \approx -3.464\), \(y = 4\sin\tfrac{5\pi}{6} = 4 \times \tfrac12 = 2\).
Curves. \(r = 3\) is a circle (compare \(x^2 + y^2 = 9\)). \(r = \theta\) is a spiral. \(r = 1 + \cos\theta\) is a heart-shaped cardioid, which is also the pickup pattern of a cardioid microphone.
13.7.4. Where you’d use this
Radar and sonar; robot arm kinematics (joint angles and link lengths); orbital mechanics; antenna and microphone directional patterns; and complex numbers, where \(r\) and \(\theta\) become modulus and argument (Chapter 15). Two concrete cases:
-
Games: a gamepad thumbstick reports \((x, y)\), each between −1 and 1. Pushed to \((0.6, -0.8)\), it gives \(r = \sqrt{0.36 + 0.64} = 1\) (full speed) and \(\theta = \operatorname{atan2}(-0.8, 0.6) \approx -53.1^\circ\). Games convert to polar so they can apply a dead zone (ignore \(r < 0.15\), because worn sticks drift) and a response curve on \(r\) without bending the direction.
-
Air traffic and radar: a radar reports an aircraft at 40 km on a bearing of 060°. With east as \(x\) and north as \(y\) (bearings run clockwise from north), its position is \((40\sin 60^\circ, 40\cos 60^\circ) \approx (34.6, 20.0)\) km. Every radar display does this polar-to-Cartesian conversion for each blip on every sweep.
|
Polar sketcher
Type \(r = f(\theta)\) (for example \(\cos(k\theta)\) with \(k\) on a slider) and watch the curve trace out as \(\theta\) sweeps from 0 to \(2\pi\), with the radius line drawn live. Integer \(k\) gives roses with \(k\) or \(2k\) petals. Find the rule. Try it: Rose curves r = cos(kθ) |
13.8. Waves and oscillation
13.8.1. The problem
Pull a mass on a spring down and let go. It bounces up and down with a regular rhythm. A pendulum, a guitar string, the air pressure at your eardrum and the voltage in a wall socket all do something similar. What function describes this motion, and why that one?
13.8.2. Simple harmonic motion
If you studied Mechanics, you met simple harmonic motion (SHM): motion in which the restoring force is proportional to the displacement and directed back towards equilibrium. For a spring with stiffness \(k\) and mass \(m\), that’s Hooke’s law with Newton’s second law:
where \(\ddot{x}\) (read "x double-dot") is the acceleration, the second derivative of position with respect to time (Chapter 17). The solution is a sinusoid:
Why does a sinusoid appear? Think of the unit circle again. A point moving round a circle at a steady angular speed \(\omega\) has \(x\)-coordinate \(\cos(\omega t)\), and its acceleration always points to the centre, so the \(x\)-component of the acceleration is proportional to \(-x\). SHM is the shadow of uniform circular motion. A cylinder on a turntable lit from the side casts a shadow that moves in exactly simple harmonic motion. Try it: Watch SHM as a shadow.
|
Simple harmonic motion
Simple harmonic motion is oscillation described by \(x(t) = A\cos(\omega t + \varphi)\).
The period of SHM doesn’t depend on the amplitude. A small bounce and a big bounce take the same time. |
Worked example. A 0.5 kg mass hangs on a spring with \(k = 200\) N/m. It is pulled 4 cm below equilibrium and released. Try it: Run the spring example.
The pendulum. For a pendulum of length \(L\), the restoring force is proportional to \(\sin\theta\), not \(\theta\), so the motion isn’t exactly SHM. For small swings, though, \(\sin\theta \approx \theta\) (in radians, Section 13.2), and the motion becomes SHM with \(\omega = \sqrt{g/L}\), giving period \(T = 2\pi\sqrt{L/g}\). A 1 m pendulum has \(T = 2\pi\sqrt{1/9.81} \approx 2.006\) s, which is why a "seconds pendulum" (one second per swing) is about a metre long.
13.8.3. Waves
A wave is an oscillation that travels. A sinusoidal wave moving along a string is
where \(k = 2\pi/\lambda\) is the wavenumber (\(\lambda\), lambda, is the wavelength) and the wave travels at speed \(v = \omega/k = f\lambda\). Sound in air at 343 m/s with frequency 440 Hz (concert A) has wavelength \(343/440 \approx 0.78\) m.
13.8.4. Sound, and adding sines
A pure sine wave sounds like a tuning fork: clean and rather dull. Real instruments produce a fundamental frequency plus harmonics at whole-number multiples of it, each with its own amplitude. The mix of harmonics is what makes a violin sound different from a clarinet at the same pitch. Try it: Spectrum of three sines.
13.8.5. Where you’d use this
-
Noise-cancelling headphones: a microphone picks up the engine drone, say \(0.2\sin(2\pi \cdot 120\, t)\), and the headphones play the same wave shifted by half a period, \(0.2\sin(2\pi \cdot 120\, t + \pi) = -0.2\sin(2\pi \cdot 120\, t)\). The two add to zero at your ear. Steady low hums cancel well; sudden sounds don’t, because the electronics can’t predict them in time.
-
Earthquakes and buildings: a building sways like a mass on a spring with its own natural frequency \(f = \omega/2\pi\). If ground shaking arrives near that frequency, each push lands in step with the swing and the amplitude grows (resonance). Engineers estimate a building’s natural period at roughly 0.1 s per storey, so a 10-storey block (about 1 s) is most at risk from shaking at about 1 Hz.
-
Computing: Wi-Fi and 4G encode bits by changing the amplitude and phase of sine-wave carriers, so each received symbol is decoded by asking "what are \(A\) and \(\varphi\) for this short burst?". The same parameters from Section 13.4 carry your data.
|
Fourier’s big idea
In 1807 Joseph Fourier claimed that any periodic signal, even a square wave with sharp corners, can be written as a sum of sines and cosines at multiples of a base frequency. His contemporaries, Lagrange among them, were sceptical. The claim turned out (with careful conditions) to be true, and Fourier analysis is now the basis of audio compression (MP3, AAC), image compression (JPEG uses a cosine transform), MRI, radio, spectrum analysers and fast polynomial multiplication. The Fast Fourier Transform (FFT, 1965) is often listed among the most important algorithms of the 20th century. |
13.9. Trigonometry in computing
13.9.1. Rotating a point in 2D
The problem. A game sprite at \((x, y)\) relative to its pivot has to rotate by angle \(\theta\). Where does it end up?
Derivation. Write the point in polar form: \(x = r\cos\varphi\), \(y = r\sin\varphi\), with \(r\) its distance from the origin and \(\varphi\) its current angle. Rotating keeps \(r\) the same and adds \(\theta\) to the angle, so the new point is
|
2D rotation formula
Rotating \((x, y)\) anticlockwise about the origin by \(\theta\):
\[x' = x\cos\theta - y\sin\theta, \qquad y' = x\sin\theta + y\cos\theta.\]
In Chapter 27 this becomes multiplication by the rotation matrix \(\begin{bmatrix}\cos\theta & -\sin\theta \\ \sin\theta & \cos\theta\end{bmatrix}\). To rotate about some other point \((c_x, c_y)\), translate it to the origin, rotate, and translate back. |
Worked example. Rotate \((2, 0)\) by 30°: \(x' = 2\cos 30^\circ - 0 = \sqrt3 \approx 1.732\), \(y' = 2\sin 30^\circ + 0 = 1\). The distance is still \(\sqrt{3 + 1} = 2\). Correct.
Worked example. Rotate \((3, 1)\) by 90°: \(\cos 90^\circ = 0\), \(\sin 90^\circ = 1\), so \(x' = -1\), \(y' = 3\). Rotating by 90° sends \((x, y) \mapsto (-y, x)\), a trick worth remembering for perpendicular vectors.
import math
def rotate(x, y, theta, cx=0.0, cy=0.0):
"""Rotate (x, y) anticlockwise by theta radians about (cx, cy)."""
c, s = math.cos(theta), math.sin(theta)
dx, dy = x - cx, y - cy # translate pivot to origin
return (cx + dx * c - dy * s, # rotate, translate back
cy + dx * s + dy * c)
print(rotate(2, 0, math.radians(30)))
print(rotate(3, 1, math.radians(90)))
print(rotate(3, 1, math.radians(50)))
print(rotate(5, 5, math.pi, cx=4, cy=4)) # half-turn about (4, 4)
(1.7320508075688774, 0.9999999999999999) (-0.9999999999999998, 3.0) (1.1623183859406403, 2.9409209390434734) (3.0, 3.0)
Notice 0.9999999999999999 and -0.9999999999999998. Neither \(\pi/6\) nor \(\pi/2\) is exactly representable as a float, so math.cos(math.radians(90)) is about \(6 \times 10^{-17}\), not 0. Compare floating-point trig results with a tolerance (math.isclose), never with ==.
13.9.2. atan2: why it exists
The problem. An enemy at \((e_x, e_y)\) has to turn to face the player at \((p_x, p_y)\). The direction vector is \((d_x, d_y) = (p_x - e_x, p_y - e_y)\), and the facing angle \(\theta\) satisfies \(\tan\theta = d_y/d_x\). So is the answer atan(dy/dx)? Try it: Watch atan aim the wrong way.
No, for two reasons:
-
Lost quadrant. \((2, 1)\) and \((-2, -1)\) have the same ratio \(1/2\), so
atanreturns 26.6° for both. One of them actually points the opposite way (−153.4°). The division throws away the signs. -
Division by zero. Straight up or down, \(d_x = 0\).
atan2(y, x) takes the two components separately. It uses their signs to pick the correct quadrant and returns an angle in \((-\pi, \pi]\) (−180° to 180°). It handles \(x = 0\) without trouble. Note the argument order: y first, matching \(\tan^{-1}(y/x)\).
import math
for x, y in [(2, 1), (-2, -1), (-2, 1), (0, 3), (0, -3)]:
naive = math.degrees(math.atan(y / x)) if x != 0 else float("nan")
good = math.degrees(math.atan2(y, x))
print(f"({x:>2}, {y:>2}) atan(y/x) = {naive:8.2f} atan2(y, x) = {good:8.2f}")
( 2, 1) atan(y/x) = 26.57 atan2(y, x) = 26.57 (-2, -1) atan(y/x) = 26.57 atan2(y, x) = -153.43 (-2, 1) atan(y/x) = -26.57 atan2(y, x) = 153.43 ( 0, 3) atan(y/x) = nan atan2(y, x) = 90.00 ( 0, -3) atan(y/x) = nan atan2(y, x) = -90.00
The navigation problem by components. Let east be \(x\) and north be \(y\). A bearing \(\beta\) (clockwise from north) points in the direction \((\sin\beta, \cos\beta)\). Note that this is the reverse of the usual math convention.
This matches the law-of-cosines answer and also gives the direction. Swapping the arguments to atan2 converts between the math convention (anticlockwise from east) and the compass convention (clockwise from north). Swapping them by accident is a classic bug.
13.9.3. Degrees versus radians in math libraries
import math
print(math.sin(90)) # radians! not 1
print(math.sin(math.radians(90))) # convert first
print(math.cos(math.pi / 2)) # not exactly 0
print(math.degrees(math.atan2(1, 1)))
Try it: See the degrees bug.
0.8939966636005579 1.0 6.123233995736766e-17 45.0
|
Classic trig bugs in code
|
13.9.4. Synthesising sound with sine waves
A digital audio signal is a list of samples, numbers taken at a fixed sample rate (44,100 per second for CD audio). A pure tone of frequency \(f\) Hz and amplitude \(A\) is
where \(f_s\) is the sample rate and \(n/f_s\) is the time of sample \(n\) in seconds. Here is a complete, standard-library-only synthesiser that writes a one-second chord (A3, its octave, and a fifth above that) to a WAV file:
import math, struct, wave
RATE = 44_100 # samples per second
partials = [(220, 0.5), (440, 0.25), (660, 0.125)] # (Hz, amplitude)
frames = bytearray()
for n in range(RATE): # one second
t = n / RATE
s = sum(a * math.sin(2 * math.pi * f * t) for f, a in partials)
frames += struct.pack("<h", int(32767 * s)) # 16-bit signed sample
with wave.open("chord.wav", "wb") as w:
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(RATE)
w.writeframes(bytes(frames))
print(len(frames) // 2, "samples written")
44100 samples written
The amplitudes add up to 0.875, so the sum never goes past full scale and doesn’t clip.
13.9.5. A Fourier teaser: finding the sines inside a signal
Fourier analysis runs synthesis in reverse: given the summed waveform, recover which frequencies are in it and how strong each one is. NumPy’s FFT does this in \(O(n\log n)\) time. (That’s the logarithm from Chapter 12 again.) Try it: See the spectrum live; Find the three sines.
import numpy as np
RATE = 8000
t = np.arange(RATE) / RATE # one second of samples
signal = (0.5 * np.sin(2 * np.pi * 220 * t)
+ 0.25 * np.sin(2 * np.pi * 440 * t)
+ 0.125 * np.sin(2 * np.pi * 660 * t))
spectrum = np.abs(np.fft.rfft(signal)) * 2 / len(signal) # amplitude per frequency
freqs = np.fft.rfftfreq(len(signal), d=1 / RATE)
for i in np.argsort(spectrum)[-3:][::-1]:
print(f"{freqs[i]:6.1f} Hz amplitude {spectrum[i]:.3f}")
Try it: Run this in your browser.
220.0 Hz amplitude 0.500 440.0 Hz amplitude 0.250 660.0 Hz amplitude 0.125
The FFT recovers exactly the three sines that went in. In Chapter 15 you’ll see why it uses complex numbers: Euler’s formula \(e^{i\theta} = \cos\theta + i\sin\theta\) packs a sine and a cosine into one exponential, which ties this chapter to the previous one.
|
Build a synth
Sliders for the amplitudes of the first eight harmonics of a 220 Hz tone. Show the summed waveform and play it live. Presets: odd harmonics with amplitude \(1/k\) (approaching a square wave), all harmonics with \(1/k\) (a sawtooth). Then show the FFT bar chart of the result so synthesis and analysis appear side by side. |
|
Rotate and aim
A turret at the centre of the screen tracks the mouse. Toggle between Try it: Rotate and aim |
13.10. Interactive tools
Browser tools on the companion website that go with this chapter. Each example link in the chapter opens one of them with that example already set up.
13.11. Exercises
Recognition
-
In a right triangle, the side opposite \(\theta\) is 5 and the hypotenuse is 13. Find \(\sin\theta\), \(\cos\theta\), \(\tan\theta\).
-
Convert to radians: 45°, 210°, −90°. Convert to degrees: \(\tfrac{2\pi}{3}\), \(\tfrac{7\pi}{4}\), 1.
-
State the sign of: \(\sin 200^\circ\), \(\cos 100^\circ\), \(\tan 300^\circ\), \(\cos(-50^\circ)\).
-
Give amplitude, period, phase shift and midline of \(y = 4\cos(3x - \pi) - 1\).
Application
-
A kite string is 60 m long and makes 52° with the ground. How high is the kite (ignore sag)?
-
Find exact values: \(\sin 300^\circ\), \(\cos\tfrac{3\pi}{4}\), \(\tan 225^\circ\), \(\cos 15^\circ\).
-
Solve \(2\cos x + 1 = 0\) for \(0 \le x < 2\pi\).
-
\(\cos\theta = -\tfrac{5}{13}\), \(\theta\) in quadrant III. Find \(\sin\theta\), \(\tan\theta\) and \(\sin 2\theta\).
-
Triangle with \(a = 8\), \(b = 11\), \(C = 38^\circ\). Find \(c\).
-
Convert \((-1, -\sqrt3)\) to polar coordinates with \(\theta \in (-\pi, \pi]\). Convert \((r, \theta) = (6, -\tfrac{\pi}{3})\) to Cartesian.
Problem solving
-
From the top of a 40 m lighthouse, the angle of depression to a boat is 7°. How far is the boat from the base of the lighthouse?
-
Rotate the triangle with vertices \((1, 0)\), \((3, 0)\), \((3, 2)\) by 45° anticlockwise about \((1, 0)\). Give exact coordinates.
-
Prove the identity \(\dfrac{\sin 2x}{1 + \cos 2x} = \tan x\).
-
A Ferris wheel of radius 20 m has its centre 22 m above the ground and turns once every 4 minutes. A rider boards at the bottom at \(t = 0\). Write their height \(h(t)\) (metres, \(t\) in minutes) and find the first time they’re 32 m up.
-
A function
heading_to(dx, dy)must return a compass bearing in [0°, 360°) (clockwise from north) for a screen where \(y\) points down and north is up the screen. Write it. -
What length does a pendulum need for a period of exactly 1 s? (\(g = 9.81\ \text{m/s}^2\).)
Answers
-
Adjacent \(= \sqrt{13^2 - 5^2} = 12\). \(\sin\theta = \tfrac{5}{13}\), \(\cos\theta = \tfrac{12}{13}\), \(\tan\theta = \tfrac{5}{12}\).
-
\(\tfrac{\pi}{4}\), \(\tfrac{7\pi}{6}\), \(-\tfrac{\pi}{2}\); 120°, 315°, \(180/\pi \approx 57.30^\circ\).
-
200° is in quadrant III, so sin is negative. 100° is in II, so cos is negative. 300° is in IV, so tan is negative. Cosine is even, so \(\cos(-50^\circ) = \cos 50^\circ\) is positive.
-
\(4\cos\big(3(x - \tfrac{\pi}{3})\big) - 1\): amplitude 4, period \(\tfrac{2\pi}{3}\), shift \(\tfrac{\pi}{3}\) to the right, midline \(y = -1\).
-
\(h = 60\sin 52^\circ = 60 \times 0.788011 \approx 47.3\) m.
-
\(\sin 300^\circ = -\sin 60^\circ = -\tfrac{\sqrt3}{2}\); \(\cos 135^\circ = -\tfrac{\sqrt2}{2}\); \(\tan 225^\circ = +\tan 45^\circ = 1\); \(\cos(45^\circ - 30^\circ) = \tfrac{\sqrt2}{2}\cdot\tfrac{\sqrt3}{2} + \tfrac{\sqrt2}{2}\cdot\tfrac12 = \tfrac{\sqrt6 + \sqrt2}{4} \approx 0.9659\).
-
\(\cos x = -\tfrac12\). Reference angle \(\tfrac{\pi}{3}\), cosine negative in II and III: \(x = \pi - \tfrac{\pi}{3} = \tfrac{2\pi}{3}\) and \(x = \pi + \tfrac{\pi}{3} = \tfrac{4\pi}{3}\).
-
\(\sin^2\theta = 1 - \tfrac{25}{169} = \tfrac{144}{169}\), and sine is negative in III, so \(\sin\theta = -\tfrac{12}{13}\). \(\tan\theta = \tfrac{-12/13}{-5/13} = \tfrac{12}{5}\). \(\sin 2\theta = 2\sin\theta\cos\theta = 2(-\tfrac{12}{13})(-\tfrac{5}{13}) = \tfrac{120}{169}\).
-
\(c^2 = 64 + 121 - 176\cos 38^\circ = 185 - 176 \times 0.788011 = 185 - 138.69 = 46.31\), so \(c \approx 6.81\).
-
\(r = \sqrt{1 + 3} = 2\). The point is in quadrant III: \(\theta = \operatorname{atan2}(-\sqrt3, -1) = -\tfrac{2\pi}{3}\). Second part: \(x = 6\cos(-\tfrac{\pi}{3}) = 3\), \(y = 6\sin(-\tfrac{\pi}{3}) = -3\sqrt3 \approx -5.196\).
-
The angle of depression from the top equals the angle of elevation from the boat (alternate angles), so \(\tan 7^\circ = 40/d\) and \(d = 40/\tan 7^\circ = 40/0.122785 \approx 325.8\) m.
-
Translate by \((-1, 0)\): the vertices become \((0,0), (2,0), (2,2)\). Rotate by 45° using \(\cos = \sin = \tfrac{\sqrt2}{2}\): \((0,0)\); \((2,0) \mapsto (\sqrt2, \sqrt2)\); \((2,2) \mapsto (2\tfrac{\sqrt2}{2} - 2\tfrac{\sqrt2}{2},\ 2\tfrac{\sqrt2}{2} + 2\tfrac{\sqrt2}{2}) = (0, 2\sqrt2)\). Translate back: \((1, 0)\), \((1 + \sqrt2, \sqrt2)\), \((1, 2\sqrt2)\).
-
Left side: \(\dfrac{2\sin x\cos x}{1 + (2\cos^2 x - 1)} = \dfrac{2\sin x\cos x}{2\cos^2 x} = \dfrac{\sin x}{\cos x} = \tan x\) (where defined).
-
\(h(t) = 22 - 20\cos\!\left(\tfrac{2\pi}{4}t\right) = 22 - 20\cos\!\left(\tfrac{\pi t}{2}\right)\). Check: \(h(0) = 2\), the bottom. For 32 m: \(\cos(\pi t/2) = -\tfrac12\), so \(\pi t/2 = \tfrac{2\pi}{3}\) and \(t = \tfrac43\) minutes (80 s).
-
North is up the screen, which is \(-y\) in screen coordinates, and east is \(+x\). Bearing \(= \operatorname{atan2}(\text{east}, \text{north}) = \operatorname{atan2}(dx, -dy)\). In Python:
return math.degrees(math.atan2(dx, -dy)) % 360. -
\(T = 2\pi\sqrt{L/g} \Rightarrow L = g\left(\tfrac{T}{2\pi}\right)^2 = 9.81/(4\pi^2) \approx 0.2485\) m.
13.12. Mastery check
-
A tree casts a 25 m shadow when the Sun is 40° above the horizon. How tall is it?
-
Without a calculator: \(\sin\tfrac{7\pi}{6}\), \(\cos(-\tfrac{\pi}{4})\), \(\tan\tfrac{2\pi}{3}\).
-
Explain in one or two sentences why \(\sin^2\theta + \cos^2\theta = 1\) holds for every angle.
-
Use the angle-sum formula to find the exact value of \(\cos 105^\circ\).
-
Triangle with sides 7, 8, 5: find the largest angle.
-
Rotate \((4, -2)\) by 90° clockwise about the origin.
-
Why does
math.atan2(-1, -1)give a different answer frommath.atan(-1 / -1)? Give both answers in degrees. -
Write a sinusoid with amplitude 3, period 0.01 s, midline 0, starting at 0 and rising at \(t = 0\). What is its frequency?
Decision rule: All correct without hesitation → move on to Chapter 14 or Chapter 15. Stuck on 1–2 → reread Section 13.1 and Section 13.3. Stuck on 3–4 → reread Section 13.5. Stuck on 5 → reread Section 13.6. Stuck on 6–7 → reread Section 13.9. Stuck on more → work the section exercises first.
Mastery check answers
-
\(h = 25\tan 40^\circ = 25 \times 0.839100 \approx 20.98\) m.
-
\(\tfrac{7\pi}{6} = 210^\circ\), quadrant III: \(-\tfrac12\). Cosine is even: \(\tfrac{\sqrt2}{2}\). \(120^\circ\), quadrant II: \(-\tan 60^\circ = -\sqrt3\).
-
\((\cos\theta, \sin\theta)\) is by definition a point on the unit circle \(x^2 + y^2 = 1\), whatever the angle.
-
\(\cos(60^\circ + 45^\circ) = \tfrac12\cdot\tfrac{\sqrt2}{2} - \tfrac{\sqrt3}{2}\cdot\tfrac{\sqrt2}{2} = \tfrac{\sqrt2 - \sqrt6}{4} \approx -0.2588\).
-
The largest angle is opposite the side of length 8: \(\cos\theta = \dfrac{7^2 + 5^2 - 8^2}{2 \cdot 7 \cdot 5} = \dfrac{10}{70} = \tfrac17\), so \(\theta = \cos^{-1}(1/7) \approx 81.79^\circ\).
-
Clockwise 90° is \(\theta = -90^\circ\): \(x' = x\cos(-90^\circ) - y\sin(-90^\circ) = 0 - (-2)(-1) = -2\), \(y' = x\sin(-90^\circ) + y\cos(-90^\circ) = -4 + 0 = -4\). The answer is \((-2, -4)\). In general a clockwise quarter-turn sends \((x, y) \mapsto (y, -x)\).
-
\(-1/-1 = 1\), so
atansees only the ratio and returns 45°.atan2sees both negative signs, places the point in quadrant III, and returns −135°. -
Frequency \(f = 1/0.01 = 100\) Hz, \(\omega = 2\pi f = 200\pi\), so \(y = 3\sin(200\pi t)\).