In many countries, railways, including intercity, urban railway (metro), tram, monorail, and other lines, have played a vital role in passenger and freight transportation in the past. The problem of optimizing energy and reducing air pollution has always been a very important issue in transportation systems. Given that trains are faster and more accessible for passenger and freight transportation and consume less energy compared to other public transportation vehicles, the use of railway lines has gained significant attention from many countries, individuals, and various companies. Nowadays, with the limitation of resources, the importance of optimal energy consumption has received a lot of attention from researchers, and various methods have been proposed in this regard. As is well known, trains travel on railway lines based on speed profiles and control tables designed by the interlocking system, which have a close relationship with energy consumption. In recent years, investment in providing methods for designing train speed profiles with the aim of optimizing energy consumption in railway transportation systems has increased. There are various methods for optimizing speed profiles, among which the use of optimal control theory can be mentioned. In this article, the multiple-phase optimal control theory has been used to design train speed profiles. First, the train movement is divided into several phases based on the route layout, and then for each phase, dynamic equations and cost functions have been written to optimize energy consumption. In the next step, the solution to this multiple-phase problem will be done using pseudo-spectral methods and the GPOPS software. Finally, the designed speed profile for trains has been used for the Tehran-Mashhad path in the Abardej-Kavir section, and the level of energy consumption and optimization using this method has been discussed and investigated.