Solution (source code)

= Solution

The <sine> and <cosine> functions are orthogonal over a complete period. For positive integers $m,n$,
$$
\int_0^T\cos(m\omega t)\cos(n\omega t)\,dt
=\int_0^T\sin(m\omega t)\sin(n\omega t)\,dt
=\frac T2\delta_{mn},
$$
while every mixed sine-cosine integral and every integral of a nonconstant single harmonic is zero. These identities follow from the product-to-sum formulas and the integer-period endpoint values.

Multiply the <Fourier series> by $\cos(m\omega t)$ and integrate. All terms except $a_m\cos^2(m\omega t)$ vanish, giving $Ta_m/2$. Repeating with <sine> isolates $b_m$. Thus
$$
\boxed{a_n=\frac2T\int_0^T V_1(t)\cos(n\omega t)\,dt,\qquad
b_n=\frac2T\int_0^T V_1(t)\sin(n\omega t)\,dt},\quad n\geq1.
$$
Integrating without a harmonic also gives $a_0=(2/T)\int_0^T V_1(t)\,dt$. The factor $1/2$ on the constant term in the chosen convention is essential.