Dynamic solution for an OptimalControl Problem