The variational calculation of energies and widths of resonances