sufficient condition for a maximum is the concavity of the Hamiltonian evaluated at the solution, i.e. where is the optimal control, and is resulting optimal trajectory for the state variable. Alternatively, by a result due to Olvi L. Mangasarian, the necessary conditions are sufficient if the functions and are both concave in and . Derivation from the Lagrangian A constrained optimization problem as the one stated above usually suggests a Lagrangian expression, specifically where compares to the Lagrange multiplier in a static optimization problem but is now, as noted above, a function of time. In order to eliminate , the last term on the right-hand side can be rewritten using integration by parts, such that which can be substituted back into the Lagrangian expression to give To derive the first-order conditions for an optimum, assume that the solution has been found and the Lagrangian is maximized. Then any perturbation to or must cause the value of the Lagrangian to decline. Specifically, the total derivative of obeys For this expression to equal zero necessitates the following optimality conditions: If both the initial value and terminal value are fixed, i.e. , no conditions on and are needed. If the terminal value is free, as is often the case, the additional condition is necessary for optimality. The latter is called a transversality condition for a fixed horizon problem. It can be seen that the necessary conditions are identical to the ones stated above for the Hamiltonian. Thus the Hamiltonian can be understood as a device to generate the first-order necessary conditions. The Hamiltonian in discrete time When the problem is formulated in discrete time, the Hamiltonian is defined as: and the costate equations are (Note that the discrete time Hamiltonian at time involves the costate variable at time This small detail is essential so that when we differentiate with respect to we get a term involving on the right hand side of the costate equations. Using a wrong convention here can lead to incorrect results, i.e. a costate equation which is not a backwards difference equation). Behavior of the Hamiltonian over time From Pontryagin's maximum principle, special conditions for the Hamiltonian can be derived. When the final time is fixed and the Hamiltonian does not depend explicitly on time , then: or if the terminal time is free, then: Further, if the terminal time tends to infinity, a transversality condition on the Hamiltonian