Trigonometric Functions
The trigonometric functions sine, cosine and tangent turn an angle into a number. In calculus they matter because they are the basic periodic functions: anything that repeats, from a pendulum to a sound wave to a spinning wheel, is described with sines and cosines.
Radians
Calculus measures angles in radians. One radian is the angle at which the arc length on a circle equals its radius. A full turn is radians, so
| Degrees | 0° | 30° | 45° | 60° | 90° | 180° | 270° | 360° |
|---|---|---|---|---|---|---|---|---|
| Radians | 0 |
Radians are not just a convention: the clean rule "the derivative of is " holds only when is in radians. Python's math.sin and np.sin expect radians too; convert with math.radians or np.deg2rad.
The unit circle definition
Walk an angle counterclockwise from the positive -axis around the circle of radius 1. The point you reach is
From that picture:
| Function | Definition | Period | Range | Even / odd |
|---|---|---|---|---|
| -coordinate | odd | |||
| -coordinate | even | |||
| all reals | odd | |||
| even | ||||
| odd | ||||
| all reals | odd |
is undefined where , at ; its graph has vertical asymptotes there.
import math
print(" deg rad sin cos tan")
for deg in [0, 30, 45, 60, 90, 120, 180]:
r = math.radians(deg)
s, c = math.sin(r), math.cos(r)
t = "undef" if abs(c) < 1e-12 else f"{s / c:7.4f}"
print(f"{deg:4} {r:6.4f} {s:7.4f} {c:7.4f} {t:>7}") deg rad sin cos tan
0 0.0000 0.0000 1.0000 0.0000
30 0.5236 0.5000 0.8660 0.5774
45 0.7854 0.7071 0.7071 1.0000
60 1.0472 0.8660 0.5000 1.7321
90 1.5708 1.0000 0.0000 undef
120 2.0944 0.8660 -0.5000 -1.7321
180 3.1416 0.0000 -1.0000 -0.0000
The -0.0000 for is floating-point noise: comes out as about , not exactly 0, and dividing by −1 gives a tiny negative number.
Key identities
SymPy can confirm them:
import sympy as sp
a, b, t = sp.symbols('a b t')
print(sp.simplify(sp.sin(t)**2 + sp.cos(t)**2))
print(sp.expand_trig(sp.sin(a + b)))
print(sp.expand_trig(sp.cos(2*t)))
print(sp.simplify(sp.sin(t + sp.pi/2)))
print(sp.solve(sp.sin(t) - sp.Rational(1, 2), t))1
sin(a)*cos(b) + sin(b)*cos(a)
2*cos(t)**2 - 1
cos(t)
[pi/6, 5*pi/6]
sp.solve returns the solutions in one period only. The full solution of is or for any whole number .
Inverse trig functions
Because sine and cosine repeat, they are not one-to-one. Their inverses use restricted ranges:
| Inverse | Domain | Returns angles in |
|---|---|---|
(asin) |
||
(acos) |
||
(atan) |
all reals |
Programmers usually want math.atan2(y, x) instead of atan(y/x): it looks at the signs of both coordinates and returns the correct angle in all four quadrants, in (atan2).
In software: waves and animation
A sine wave has amplitude , frequency (cycles per second) and phase .
- Audio: a 440 Hz sine sampled 44,100 times per second is the note A. Every sound is a sum of sines, the idea behind the FFT.
- Animation: bobbing pickups, pulsing buttons and breathing idle animations use
sin(time); circular motion uses . - Graphics: 2D rotation by uses the matrix built from and (2D Rotation).
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 0.01, 1000, endpoint=False) # 10 ms
a4 = 0.8 * np.sin(2 * np.pi * 440 * t) # the note A4
e5 = 0.4 * np.sin(2 * np.pi * 659.25 * t) # the note E5
chord = a4 + e5
plt.figure(figsize=(6, 4))
plt.plot(t * 1000, a4, label="440 Hz", alpha=0.6)
plt.plot(t * 1000, e5, label="659.25 Hz", alpha=0.6)
plt.plot(t * 1000, chord, label="sum", linewidth=2)
plt.xlabel("time (ms)"); plt.legend(); plt.grid(alpha=0.3)
plt.title("Two sine waves and their sum")
print("440 Hz period:", round(1000 / 440, 4), "ms")
print("peak of the sum:", round(chord.max(), 4))440 Hz period: 2.2727 ms
peak of the sum: 1.1487
The peak of the sum is less than because the two waves rarely reach their maximum at the same instant.
Exercises
Exercise 1: Convert angles
Convert 150° and 225° to radians, and and 2 rad to degrees.
Answer
, , , .
Solution
Exercise 2: Exact values
Find , and exactly using the unit circle.
Answer
, , .
Solution
- is 150°, in quadrant II with reference angle 30°: sine is positive, .
- is 240°, in quadrant III with reference angle 60°: cosine is negative, .
- is 135°, quadrant II, reference 45°: , , so .
import sympy as sp
print(sp.sin(5*sp.pi/6), sp.cos(4*sp.pi/3), sp.tan(3*sp.pi/4))1/2 -1/2 -1
Exercise 3: Use an identity
If and is in quadrant II, find , and .
Answer
, , .
Solution
Exercise 4: Orbiting sprite
A sprite circles the point with radius 20, one turn every 4 seconds, starting at angle 0. Write its position at time and print it at .
Answer
: , , , .
Solution
One turn () every 4 s gives angular speed rad/s.
import math
def pos(t):
a = math.pi / 2 * t
return (round(100 + 20 * math.cos(a), 6) + 0.0, round(50 + 20 * math.sin(a), 6) + 0.0)
for t in range(4):
print(t, pos(t))0 (120.0, 50.0)
1 (100.0, 70.0)
2 (80.0, 50.0)
3 (100.0, 30.0)
Test yourself
What is the period of ?
- It is not periodic
Why should you prefer math.atan2(y, x) over math.atan(y / x) for the angle of a vector?
- atan2 is faster
- atan2 returns degrees
- atan2 uses the signs of and to get the right quadrant and handles
- atan cannot take negative numbers
Which identity is correct?