Optimization-based Solutions to Constrained Trajectory-tracking and Path-following Problems