Analytical and numerical modelling of undulatory locomotion for limbless organisms in granular/viscous media