Trigonometric Integrals
Integrals of powers and products of sines, cosines, tangents and secants follow a small number of patterns, each built on a Pythagorean identity or a double-angle formula plus a substitution. This lesson collects those patterns, proves the orthogonality relations they lead to, and shows why those relations are the foundation of Fourier analysis: audio codecs, JPEG and signal processing all rely on them.
Powers of sine and cosine:
| Case | Strategy | Identity used |
|---|---|---|
| odd | save one , convert the rest to sines, | |
| odd | save one , convert the rest to cosines, | |
| both even | lower the powers with half-angle formulas | , |
Odd power.
Both even.
import sympy as sp
x = sp.symbols('x')
mine = {
sp.sin(x)**2 * sp.cos(x)**3: sp.sin(x)**3 / 3 - sp.sin(x)**5 / 5,
sp.cos(x)**4: 3*x/8 + sp.sin(2*x)/4 + sp.sin(4*x)/32,
}
for f, F in mine.items():
print(f"∫ {f} dx")
print(" SymPy :", sp.integrate(f, x))
print(" ours :", F, " derivative matches:", sp.simplify(sp.diff(F, x) - f) == 0)∫ sin(x)**2*cos(x)**3 dx
SymPy : -sin(x)**5/5 + sin(x)**3/3
ours : -sin(x)**5/5 + sin(x)**3/3 derivative matches: True
∫ cos(x)**4 dx
SymPy : 3*x/8 + sin(x)*cos(x)**3/4 + 3*sin(x)*cos(x)/8
ours : 3*x/8 + sin(2*x)/4 + sin(4*x)/32 derivative matches: True
For SymPy writes its answer with powers of and instead of multiple angles. Both are antiderivatives (here they are in fact equal), and in general any two differ by a constant.
Tangent and secant:
The identities are , and .
| Case | Strategy |
|---|---|
| even () | save , convert the rest to tangents, |
| odd | save , convert the rest to secants, |
| otherwise | identities plus parts; often needs |
Two integrals to know:
is the classic case that needs parts and solving for the integral: .
Products with different frequencies
For and similar, the product-to-sum identities turn a product into a sum:
Orthogonality
For integers , integrating over a full period:
Proof (first line, ). By product-to-sum, the integrand is . For any nonzero integer , . For , the first term is , contributing . The third line holds because is odd. ∎
Treat functions on as vectors with the "dot product" . Then the sines and cosines are mutually perpendicular, just like the axes of . A numerical check of the Gram matrix:
import numpy as np
from scipy.integrate import quad
basis = [("sin x", lambda x: np.sin(x)), ("sin 2x", lambda x: np.sin(2 * x)),
("cos x", lambda x: np.cos(x)), ("cos 3x", lambda x: np.cos(3 * x))]
print(" " * 8 + "".join(f"{n:>9}" for n, _ in basis))
for n1, f in basis:
row = [quad(lambda x: f(x) * g(x), -np.pi, np.pi)[0] for _, g in basis]
print(f"{n1:>8}" + "".join(f"{v + 0.0:9.4f}" for v in np.round(row, 10))) sin x sin 2x cos x cos 3x
sin x 3.1416 0.0000 0.0000 0.0000
sin 2x 0.0000 3.1416 0.0000 0.0000
cos x 0.0000 0.0000 3.1416 0.0000
cos 3x 0.0000 0.0000 0.0000 3.1416
The diagonal is and everything else is 0.
Fourier coefficients
Because of orthogonality, the coefficients of can be read off one at a time: multiply both sides by and integrate; every term with vanishes, leaving
For the square wave , this gives for odd and 0 for even :
import numpy as np
from scipy.integrate import quad
sq = lambda x: np.sign(x)
for k in range(1, 7):
bk = quad(lambda x: sq(x) * np.sin(k * x), -np.pi, np.pi, points=[0])[0] / np.pi
print(f"b_{k} = {round(bk, 6) + 0.0:.6f} 4/(kπ) = {4 / (k * np.pi) if k % 2 else 0:.6f}")
x0 = np.pi / 2
partial = sum(4 / (k * np.pi) * np.sin(k * x0) for k in range(1, 200, 2))
print("100-term Fourier series at x = π/2:", round(float(partial), 4))b_1 = 1.273240 4/(kπ) = 1.273240
b_2 = 0.000000 4/(kπ) = 0.000000
b_3 = 0.424413 4/(kπ) = 0.424413
b_4 = 0.000000 4/(kπ) = 0.000000
b_5 = 0.254648 4/(kπ) = 0.254648
b_6 = 0.000000 4/(kπ) = 0.000000
100-term Fourier series at x = π/2: 0.9968
In software
- Audio and compression. MP3, AAC and Opus transform short windows of sound into cosine coefficients (the MDCT); JPEG does the same for 8×8 pixel blocks with the DCT. Orthogonality is what makes each coefficient independent, so small ones can be dropped.
- The FFT computes all the discrete Fourier coefficients of samples in time; the integrals above are its continuous counterpart (Fourier analysis).
- Positional encodings. Transformer models encode token positions with sines and cosines of geometric frequencies; their near-orthogonality lets the model separate positions.
Exercises
Exercise 1: Odd power of sine
Compute .
Answer
.
Solution
, , : .
Exercise 2: Even powers
Compute .
Answer
.
Solution
import sympy as sp
x = sp.symbols('x')
print(sp.integrate(sp.sin(x)**2 * sp.cos(x)**2, (x, 0, sp.pi)))pi/8
Exercise 3: Tangent and secant
Compute .
Answer
.
Solution
Save , convert : , : .
Exercise 4: A Fourier coefficient
Find for on .
Answer
.
Solution
. By parts, , so the integral is . Divide by .
Test yourself
For , what is the best first step?
- Use half-angle formulas
- Substitute
- Save one , write , substitute
- Integrate by parts
What is ?
- 0
Which identity reduces even powers of cosine?