Forward and inverse dynamics of nonholonomic mechanical systems