Optimal control of parabolic equations by spectral decomposition