Variational Methods of Construction of Optimal Grids