In the context of optimal control, when is the Hamiltonian constant?
I know that, generally, when $H$ is not explicitly a function of time, it is going to be a constant, but I just did a problem where the Hamiltonian switched from a constant value to another constant value at some later time, like a piecewise function. So, can anyone clarify when the Hamiltonian is truly a constant, and when it is a piecewise function?
We have to be more precise when talking about something being constant. A constant is a number or vector.
In optimal control theory, the Hamiltonian $\mathcal{H}$ can additionally be a function of $x(t)$, $u(t)$ and $\lambda(t)$. Hence, it is not constant. If you are only considering invariance with time then
$$\dfrac{d\mathcal{H}}{dt}=\dfrac{\partial \mathcal{H}}{\partial x}\dfrac{\partial x}{\partial t}+\dfrac{\partial \mathcal{H}}{\partial u}\dfrac{\partial u}{\partial t}+\dfrac{\partial \mathcal{H}}{\partial \lambda}\dfrac{\partial \lambda}{\partial t}$$.