A new approach to grid generation using a variational formulation