Extensions of the maximum principle and the gradient method in optimal control