Bipedal locomotion was simulated to generate a pattern of activating muscles for walking using electrical stimulation in persons with spinal cord injury (SCI) or stroke. The simulation presented in this study starts from a model of the body determined with user-specific parameters, individualized with respect to the lengths, masses, inertia, muscle and joint properties. The trajectory used for simulation was recorded from an able-bodied subject while walking with ankle-foot orthoses. A discrete mathematical model and dynamic programming were used to determine the optimal control. A cost function was selected as the sum of the squares of the tracking errors from the desired trajectories, and the weighted sum of the squares of agonist and antagonist activations of the muscle groups acting around the hip and knee joints. The aim of the simulation was to study plausible trajectories keeping in mind the limitations imposed by the spinal cord injury or stroke (e.g., spasticity, decreased range of movements in some joints, limited strength of paralyzed, externally activated muscles). If the muscles were capable of generating the movements required and the trajectory was achieved, then the simulation provided two kinds of information: 1) timing of the onset and offset of muscle activations with respect to the various gait events and 2) patterns of activation with respect to the maximum activation. These results are important for synthesizing a rule-based controller.