This paper presents a unified canonical transformationand generating function approach, including associated numerical algorithms,for linear time-varying optimal control problems with various terminalconstraints. Generating functions are employed to find the optimal controllaw by solving Hamiltonian two-point-boundary-value problems. Thetime-varying optimal control laws constructed by the second type generatingfunction do not have infinite feedback gain at terminal time, which isdifferent from other existing solutions. Motivated by practical design oftime-varying optimal control systems, a structure-preserving matrixrecursive algorithm is proposed to solve coupled time-varying matrixdifferential equations of the generating function; derivation of therecursive algorithm is based on symplectic formulation of canonicaltransformation. To preserve symplectic structure of matrices in therecursive computation, state transition matrices of the Hamiltonian systemare calculated by Magnus series. In fact, the canonical transformation andgenerating function method leads to a geometric perspective to thedesign and computation of optimal control systems.%control systems synthesis and computation.