Graphs of the Trig Functions
Plot against and you get the most famous curve in science: the sine wave. This lesson reads the shapes of the six trig graphs and shows how four numbers, amplitude, period, phase shift and vertical shift, stretch and slide a wave into any position you need.
Sine and cosine
Walk around the unit circle and record the height of the point as the angle grows. The height rises from 0 to 1 at , falls back to 0 at , reaches −1 at and returns to 0 at . Then it repeats forever.
| Property | ||
|---|---|---|
| Domain | all real numbers | all real numbers |
| Range | ||
| Period | ||
| Zeros | ||
| Maximum 1 at | ||
| Symmetry | odd: | even: |
( is any integer.) The cosine graph is the sine graph shifted left by , because .
Tangent and the reciprocal functions
is undefined wherever , that is at . Near those points the denominator approaches 0 and the graph shoots off to : these are vertical asymptotes. Between them increases through every real value. Its period is , not .
The reciprocals and have asymptotes where or is 0, and their ranges are . looks like a reflected, shifted tangent.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2 * np.pi, 2 * np.pi, 2000)
tan = np.tan(x)
tan[np.abs(tan) > 6] = np.nan # break the line at the asymptotes
plt.figure(figsize=(7, 4))
plt.plot(x, np.sin(x), label="sin x")
plt.plot(x, np.cos(x), label="cos x")
plt.plot(x, tan, label="tan x", alpha=0.7)
for k in range(-2, 2):
plt.axvline(np.pi / 2 + k * np.pi, color="gray", ls=":", lw=0.8)
plt.ylim(-3, 3); plt.axhline(0, color="black", lw=0.6)
plt.xticks(np.arange(-2, 2.5, 0.5) * np.pi, [f"{v:g}π" for v in np.arange(-2, 2.5, 0.5)])
plt.legend(loc="upper right"); plt.grid(alpha=0.3)
plt.title("sin, cos and tan (dotted lines: asymptotes of tan)")
print("tan just below π/2:", round(float(np.tan(np.pi / 2 - 1e-3)), 2))
print("tan just above π/2:", round(float(np.tan(np.pi / 2 + 1e-3)), 2))tan just below π/2: 1000.0
tan just above π/2: -1000.0
Transforming a wave
The general sinusoid is
These are the same moves as for any function in Translations, Reflections and Rotations and Composition and Inverse Functions. Example: has amplitude 3, period , starts a cycle at and oscillates between −2 and 4.
Tip: Watch out for . Factor it as : the phase shift is , not .
Reading the four numbers back from data is a common task. Here we sample a wave and recover them:
import numpy as np
x = np.linspace(0, 2 * np.pi, 100_001)
y = 3 * np.sin(2 * (x - np.pi / 4)) + 1
top, bottom = y.max(), y.min()
print("amplitude A =", round(float((top - bottom) / 2), 4))
print("midline D =", round(float((top + bottom) / 2), 4))
# period: distance between two successive upward crossings of the midline
ups = x[1:][(y[:-1] < 1) & (y[1:] >= 1)]
print("upward midline crossings:", [round(float(u / np.pi), 4) for u in ups], "(× π)")
print("period =", round(float((ups[1] - ups[0]) / np.pi), 4), "π")amplitude A = 3.0
midline D = 1.0
upward midline crossings: [0.25, 1.25] (× π)
period = 1.0 π
The first upward crossing at is the phase shift, and crossings one period apart confirm the period .
In software
Sine waves are everywhere a value must move smoothly back and forth.
- Animation. A floating pickup bobs with ; a "breathing" button scales with . Using rather than a linear back-and-forth gives a natural ease at the turning points, because the curve is flat at its peaks.
- Audio. A pure tone of frequency sampled at rate is the sequence . Concert A is 440 Hz; CD audio uses samples per second.
- Day/night cycles, tides, seasonal demand in simulations and forecasts are modelled with a sinusoid plus a midline.
- Phase lines up several waves: a row of objects with phase creates a travelling wave, like a crowd doing the wave.
import math
def bob(t, base=100.0, amp=8.0, freq=0.5, phase=0.0):
"""Height of a floating object at time t (seconds)."""
return base + amp * math.sin(2 * math.pi * freq * t + phase)
print("bobbing:", [round(bob(t), 2) for t in (0, 0.5, 1.0, 1.5, 2.0)])
# first 6 samples of a 440 Hz tone at 44.1 kHz, as 16-bit integers
R, f = 44_100, 440
samples = [round(32767 * 0.5 * math.sin(2 * math.pi * f * n / R)) for n in range(6)]
print("pcm samples:", samples)
print("samples per cycle:", round(R / f, 3))bobbing: [100.0, 108.0, 100.0, 92.0, 100.0]
pcm samples: [0, 1026, 2049, 3063, 4065, 5052]
samples per cycle: 100.227
The Waves, Sound and Signals lesson goes further, and /signals-fourier/ shows that every periodic signal is a sum of such waves.
Exercises
Exercise 1: Read the parameters
For , give the amplitude, period, midline, maximum and minimum.
Answer
Amplitude 2, period 6, midline , maximum 7, minimum 3.
Solution
Exercise 2: Phase shift trap
Find the period and phase shift of .
Answer
Period , phase shift (left by ).
Solution
Exercise 3: Build a model
A Ferris wheel of diameter 40 m has its centre 22 m above the ground and turns once every 8 minutes. A rider boards at the lowest point at . Write the height in metres and find it at and minutes.
Answer
; , .
Solution
Amplitude 20 (the radius), midline 22, period 8 so . Starting at the bottom means a flipped cosine.
import math
h = lambda t: 22 - 20 * math.cos(math.pi * t / 4)
print(round(h(0), 2), round(h(2), 2), round(h(3), 2), round(h(4), 2))2.0 22.0 36.14 42.0
Exercise 4: Asymptotes
List the vertical asymptotes of in the interval .
Answer
and .
Solution
is undefined at . With : . In that gives and : and . The period of is , so asymptotes are apart.
Exercise 5: A pulsing button
Write pulse(t) returning a scale factor that oscillates between 0.95 and 1.05 with a period of 1.2 s, starting at 1.0 and growing. Print it at .
Answer
: 1.0, 1.05, 1.0, 0.95.
Solution
Midline 1, amplitude 0.05, . A quarter period (0.3 s) reaches the peak.
import math
pulse = lambda t: 1 + 0.05 * math.sin(2 * math.pi * t / 1.2)
print([round(pulse(t), 4) for t in (0, 0.3, 0.6, 0.9)])[1.0, 1.05, 1.0, 0.95]
Test yourself
What is the period of ?
- 5
- 3
Where does have vertical asymptotes?
- At
- At
- At
- It has none
A game object bobs with . How long is one full bob?
- 0.25 s
- 4 s
- s
- 50 s