Electric propulsion system in trains has the highest efficiency compared to other propulsion systems (i.e. steam and diesel). Still, electric trains are not used on all the routes, due to the high setup and maintenance cost of the catenary system. Energy storage technologies and the battery driven trains however, make it possible to have the electric trains on the non-electrified routes as well. High energy consumption of the electric trains, makes the energy management of such trains crucial to get the best use of the energy storage device. This paper suggests an algorithm for the optimal control of the catenary free operation of an electric train equipped with an onboard energy storage device. The algorithm is based on the discrete dynamic programming and Bellman’s backward approach. The objective function is to minimize the energy consumption, i.e. having the maximum battery level left at the end of the trip. The constraints are the trip time, battery capacity, local speed limits and limitations on the traction motor. Time is the independent variable and distance, velocity and battery level are the state variables. All of the four variables are discretized which results in some inaccuracy in the calculations, which is discussed in the paper. The train model and the algorithm are based on the equations of motion which makes the model adjustable for all sorts of electric trains and energy storage devices. Moreover, any type of electrical constraints such as the ones regarding the voltage output of the energy storage device or the power output can be enforced easily, due to the nature of the dynamic programming.