Variational calculation of the electron density and potential in a thin metal film