Variational calculation of the work function for small metal spheres