We use optimal control via a distributed exterior field to steer the dynamics of an ensemble of N interacting ferromagnetic particles which are immersed into a heat bath by minimizing a quadratic functional. By using dynamic programing principle, we show the existence of a unique strong solution of the optimal control problem. By the Hopf-Cole transformation, the related Hamilton-Jacobi-Bellman equation from dynamic programming principle may be re-cast into a linear PDE on the manifold M = (S 2 ) N , whose classical solution may be represented via Feynman-Kac formula. We use this probabilistic representation for Monte-Carlo simulations to illustrate optimal switching dynamics.2000 Mathematics Subject Classification. 45K05, 46S50, 49L20, 49L25, 91A23, 93E20. 1 L 2 {F t } Ω; C [0, T ]; (S 2 ) N := m ∈ L 2 {F t } Ω; C [0, T ]; (R 3 ) N : m(t) ∈ (S 2 ) N , P-a.s. for all t ∈ [0, T ] .