A method for the automatic calculation of costates using only the resu
lts obtained from direct optimization techniques is presented. The app
roach exploits the relation between the time-varying costates and cert
ain sensitivities of the variational cost function, a relation that al
so exists between the Lagrangian multipliers obtained from a direct op
timization approach and the sensitivities of the associated nonlinear-
programming cost function. The complete theory for treating free, cont
rol-constrained, interior-point-constrained, and state-constrained opt
imal control problems is presented. As a numerical example, a state co
nstrained version of the brachistochrone problem is solved and the res
ults are compared to the optimal solution obtained from Pontryagin's m
inimum principle. The agreement is found to be excellent.