Some optimal control problems for linear and nonlinear ordinary differential equations related to the optimal switching between different magnetic fields are considered. The main aim is to move an electrical initial current by a controllable voltage in shortest time to a desired terminal current and to hold it afterwards. Necessary optimality conditions are derived by Pontryagin's principle and a formal Lagrange technique. In the case of a linear system, the principal structure of time-optimal controls is discussed. The associated optimality systems are solved by a one-shot strategy using a multigrid software package. Various numerical examples are discussed. Copyright (C) 2013 John Wiley & Sons, Ltd.