Optimal control by mathematical programming