A new method is introduced to calculate response functions within density-functional theory. It uses a conjugate-gradient algorithm applied to a variational expression from perturbation theory. The dielectric tensor, effective charges, and TO and LO phonons at q=0 in alpha-quartz are obtained. A one-parameter scissors operator gives the dielectric tensor within 0.5% of the experimental value. The anisotropy of the effective charge tensor is shown to be crucial for reproducing the LO-TO splittings.