Average Value of a Function
The average of finitely many numbers is their sum divided by their count. The average of a function over an interval, infinitely many values, is the integral divided by the length. This lesson derives that definition from ordinary averages, proves the Mean Value Theorem for Integrals, and connects averages to RMS levels in audio and electronics and to Monte Carlo integration, where integrals are computed as averages.
From sample averages to an integral
Take equally spaced sample points in , with spacing . Their average is
The right side is times a Riemann sum. Letting :
Definition. The average value of an integrable on is
.
Geometrically, is the height of the rectangle on with the same (net) area as the region under .
import numpy as np
import sympy as sp
x = sp.symbols('x', real=True)
f = x**2 - 2*x + 3
a, b = 0, 3
avg = sp.integrate(f, (x, a, b)) / (b - a)
print("exact average on [0, 3]:", avg)
fn = sp.lambdify(x, f)
for n in (4, 40, 4000):
xs = a + (np.arange(n) + 0.5) * (b - a) / n # midpoints
print(f"mean of {n:4} sample values = {float(np.mean(fn(xs))):.6f}")exact average on [0, 3]: 3
mean of 4 sample values = 2.953125
mean of 40 sample values = 2.999531
mean of 4000 sample values = 3.000000
The Mean Value Theorem for Integrals
Theorem. If is continuous on , there is a number in with
.
Proof. By the Extreme Value Theorem, has a minimum and a maximum on . The bounds property of the integral gives , so . By the Intermediate Value Theorem, the continuous takes every value between and , in particular , at some between and . ∎
(Alternatively: apply the Mean Value Theorem to ; then is exactly this statement, with in the open interval.)
For on , the average is 3, and gives (the other root, , is an endpoint).
Continuity matters: a step function that is 0 on and 1 on has average but never takes that value.
RMS: the average that measures power
The ordinary average of over a full period is 0, yet an AC current clearly delivers power. Power depends on the square of voltage, so engineers use the root mean square
For , the average of over a period is , so . A "230 V" outlet has peak voltage .
import numpy as np
V0, f0 = 325.0, 50.0 # peak volts, hertz
t = np.linspace(0, 1 / f0, 10_001)[:-1] # one period, evenly sampled
V = V0 * np.sin(2 * np.pi * f0 * t)
print(f"mean voltage = {round(float(np.mean(V)), 6) + 0.0:.6f} V") # + 0.0 turns -0.0 into 0.0
print(f"RMS voltage = {float(np.sqrt(np.mean(V**2))):.3f} V (V0/√2 = {V0 / np.sqrt(2):.3f})")mean voltage = 0.000000 V
RMS voltage = 229.810 V (V0/√2 = 229.810)
Monte Carlo: integrals as averages
Turn the definition around: . If we estimate the average by evaluating at random points, we get the Monte Carlo method. Its error shrinks like regardless of dimension, which is why it dominates in high dimensions (rendering, Bayesian statistics), even though grid rules win in one dimension:
import numpy as np
rng = np.random.default_rng(2024)
f = lambda x: np.exp(-x**2)
a, b = 0.0, 2.0
exact = 0.8820813907624215 # ∫₀² e^(−x²) dx = (√π/2)·erf(2)
for n in (100, 10_000, 1_000_000):
est = (b - a) * float(np.mean(f(rng.uniform(a, b, n))))
print(f"n = {n:>9,}: estimate = {est:.6f} error = {est - exact:+.1e}")n = 100: estimate = 1.057401 error = +1.8e-01
n = 10,000: estimate = 0.878240 error = -3.8e-03
n = 1,000,000: estimate = 0.882250 error = +1.7e-04
Each hundredfold increase in samples buys about one more correct digit (a factor of 10 in error): the law.
In software
- Audio loudness. Meters and normalisers (RMS, and LUFS in streaming services) compute the average power over a window of samples.
- Path tracing. A pixel's colour is the average of incoming light over directions; renderers estimate it with Monte Carlo samples per pixel, and the visible noise is the error.
- Moving averages and smoothing. A sliding window average over the last seconds is the continuous version of the moving averages in finance and the box blur in image processing.
Exercises
Exercise 1: Average of a power
Find the average value of on .
Answer
.
Solution
.
Exercise 2: Find c
For on , find with .
Answer
.
Solution
, which lies in .
Exercise 3: Average temperature
A day's temperature is modelled by °C for (hours). Find the average temperature.
Answer
18 °C.
Solution
The sine has period 24 h, so it completes exactly one period on and its average is 0.
import numpy as np
from scipy.integrate import quad
T = lambda t: 18 + 6 * np.sin(np.pi * (t - 9) / 12)
print(round(quad(T, 0, 24)[0] / 24, 10))18.0
Exercise 4: RMS of a sawtooth
A sawtooth wave rises linearly from to over each period. Show .
Answer
.
Solution
Over one period, take for . With : .
Test yourself
The average value of on is…
Which hypothesis does the Mean Value Theorem for Integrals need so that actually takes its average value?
- is differentiable
- is continuous on
- is positive
- is increasing
Monte Carlo integration's error decreases roughly like…