Suppose I want to minimize a functional
$\mathcal{J}[f(x)] = \frac{1}{2}\int_0^1 f(x)^2 dx \qquad (1)$
Typically we need the gradient when we solve the problem (numerically using gradient-based optimization algorithm like BFGS). In this case, its functional derivative is
$\dfrac{\delta\mathcal{J}}{\delta f} = f(x) \qquad (2)$
In my problem $f(x)$ is periodic, so I thought about expressing $f(x)$ as a Fourier sum:
$f(x) = a_0 + \sum_{n=1}\left[a_n\cos(2\pi nx) + b_n\sin(2\pi nx)\right] \qquad (3)$
So instead of discretizing $f(x)$ into $N$ points and have $N$ variables in the problem, now $\mathcal{J}$ and $\delta\mathcal{J}/\delta f$ become a function of the Fourier coefficients $(a_n,b_n)$ given by
$\mathcal{J} = \frac{1}{2}\left[ a_0^2 + \frac{1}{2}\sum_{n=1}(a_n^2 + b_n^2) \right], \qquad (4)$
$\frac{\delta\mathcal{J}}{\delta f} = \left( \frac{\partial\mathcal{J}}{\partial a_0}, ~\frac{\partial\mathcal{J}}{\partial a_1}, ~\frac{\partial\mathcal{J}}{\partial a_2}, \dots, \frac{\partial\mathcal{J}}{\partial b_1}, ~\frac{\partial\mathcal{J}}{\partial b_2}, \dots \right) \qquad (5)$
where
$\frac{\partial\mathcal{J}}{\partial a_0} = a_0, \quad \frac{\partial\mathcal{J}}{\partial a_n} = \frac{a_n}{2}, \quad \frac{\partial\mathcal{J}}{\partial b_n} = \frac{b_n}{2} \qquad (6)$.
Now there seems to be an inconsistency between Eq.(2) and Eq.(6). If the gradient is $f(x)$ (according to Eq.(2)), then I expect $\partial\mathcal{J}/\partial a_n=a_n$ and similar for $b_n$, but there is an extra factor of two. I believe the algebra is correct (correct me if I'm wrong), so where does the inconsistency arise? In the continuous case where $f(x)$ is discretized into $N$ points $f_i = f(x_i)$, then each changes to $f_i$ affects the functional 'locally', while in the Fourier case if each Fourier coefficient changes by a bit, then the whole function $f(x)$ changes shape, and the functional changes value 'globally'. Can this be the cause of the inconsistency? Or a better question to ask might be how one should interpret Eq. (6) in relation to Eq. (2)?