  We propose a method to evaluate spectral functions on the lattice based on a
variational method. On a lattice with a finite spatial extent, spectral
functions consist of discrete spectra only. Adopting a variational method, we
calculate the locations and the heights of spectral functions at low-lying
discrete spectra. We first test the method in the case of analytically solvable
free Wilson quarks at zero and finite temperatures and confirm that the method
well reproduces the analytic results for low-lying spectra. We find that we can
systematically improve the results by increasing the number of trial states. We
then apply the method to calculate the charmonium spectral functions for S and
P-wave states at zero-temperature in quenched QCD and compare the results with
those obtained using the conventional maximum entropy method (MEM). The results
for the ground state are consistent with the location and the area of the first
peak in spectral functions from the MEM, while the variational method leads to
a mass which is closer to the experimental value for the first excited state.
We also investigate the temperature dependence of the spectral functions for
S-wave states below and above $T_c$. We obtain no clear evidences for
dissociation of $J/\psi$ and $\eta_c$ up to 1.4$T_c$.
