A variational method for calculating the magnetic flux propagation in long thin ferromagnetic samples