The transition from walking to running gaits in bipedal locomotion is well known from humans. One explanation for this transition is a higher energy efficiency of running gaits at higher velocities. In this paper we use a five-link planar model of a robot to investigate the transition from walking to running based on energy efficiency. For this purpose a physically motivated cost function regarding static as well as dynamic costs is introduced. Periodic walking and running gaits are generated by means of numerical optimization to find the optimal gait of a human-like model in a range from 1.5 to 2.5 m/s. At the transition velocity walking and running require the same cost. Both gaits are investigated to identify the underlying mechanisms. The computed results correspond very well to reports from biomechanics which indicates that the model is suitable for the investigation of human locomotion as well as the generation of optimal gaits for humanoid robots.