# Numerically Stable and Computationally Efficient Expression for the Magnetic Field of a Current Loop

^{*}

## Abstract

**:**

## 1. Introduction and Motivation

## 2. Numerical Stability

#### 2.1. Fundamentals and Annotation

#### 2.2. The Classical Solution

#### 2.3. Numerical Stability of the Classical Solution

#### 2.4. Dipole Approximation

#### 2.5. Taylor Approximation

#### 2.6. Binomial Expansion

#### 2.7. An Exact and Stable Representation

#### 2.8. Loss of Precision at Sign Change

## 3. Performance

## 4. Discussion and Conclusions

## Author Contributions

## Funding

## Institutional Review Board Statement

## Informed Consent Statement

## Data Availability Statement

## Acknowledgments

## Conflicts of Interest

## Appendix A. Algorithms

#### Appendix A.1. Straightforward Implementation

#### Appendix A.2. Taylor Series Implementations

**Table A1.**Precision of various Taylor implementations in relation to the number of terms included in the respective sums.

Br_taylor_k ($\mathit{k}\le $ value) | Br_taylor_q ($\mathit{k}\ge $ value) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|

sig.figs | k-order included in series | sig.figs | q-order included in series | ||||||||

21 | 31 | 41 | 51 | 61 | 20 | 30 | 40 | 50 | 60 | ||

8 | 0.46 | 0.62 | 0.71 | 0.77 | 0.81 | 8 | 0.77 | 0.67 | 0.60 | 0.55 | 0.50 |

10 | 0.35 | 0.53 | 0.63 | 0.70 | 0.75 | 10 | 0.85 | 0.76 | 0.69 | 0.63 | 0.58 |

12 | 0.27 | 0.45 | 0.56 | 0.64 | 0.69 | 12 | 0.90 | 0.82 | 0.75 | 0.70 | 0.65 |

Bz_taylor_k ($k\le $ value) | Bz_taylor_q ($k\ge $ value) | ||||||||||

sig.figs | k-order included in series | sig.figs | q-order included in series | ||||||||

21 | 31 | 41 | 51 | 61 | 20 | 30 | 40 | 50 | 60 | ||

8 | 0.43 | 0.59 | 0.68 | 0.74 | 0.78 | 8 | 0.86 | 0.76 | 0.70 | 0.64 | 0.59 |

10 | 0.33 | 0.50 | 0.61 | 0.68 | 0.73 | 10 | 0.91 | 0.83 | 0.76 | 0.71 | 0.66 |

12 | 0.26 | 0.43 | 0.54 | 0.62 | 0.68 | 12 | 0.94 | 0.87 | 0.81 | 0.76 | 0.72 |

#### Appendix A.3. Original Implementation of Bulirsch’s cel Algorithm

#### Appendix A.4. Implementation of cel *

#### Appendix A.5. Implementation of cel **

## References

- Madenci, E.; Guven, I. The Finite Element Method and Applications in Engineering Using ANSYS®; Springer: Berlin/Heidelberg, Germany, 2015. [Google Scholar]
- Pryor, R.W. Multiphysics Modeling Using COMSOL®: A First Principles Approach; Jones & Bartlett Publishers: Burlington, MA, USA, 2009. [Google Scholar]
- Alnæs, M.; Blechta, J.; Hake, J.; Johansson, A.; Kehlet, B.; Logg, A.; Richardson, C.; Ring, J.; Rognes, M.E.; Wells, G.N. The FEniCS project version 1.5. Arch. Numer. Softw.
**2015**, 3, 9–23. [Google Scholar] - Schöberl, J. C++ 11 Implementation of Finite Elements in NGSolve; Institute for Analysis and Scientific Computing, Vienna University of Technology: Vienna, Austria, 2014; Volume 30. [Google Scholar]
- Smythe, W.B. Static and Dynamic Electricity; Hemisphere Publishing: New York, NY, USA, 1988. [Google Scholar]
- Moshier, S.L.B. Methods and Programs for Mathematical Functions; Ellis Horwood Ltd Publisher: Chichester, UK, 1989. [Google Scholar]
- ALGLIB. Available online: https://www.alglib.net/download.php (accessed on 9 June 2022).
- Wolfram, S. The Mathematica Book; Wolfram Research, Inc.: Champaign, IL, USA, 2003; Volume 1. [Google Scholar]
- MATLAB. Version 7.10.0 (R2010a); The MathWorks Inc.: Natick, MA, USA, 2010. [Google Scholar]
- Virtanen, P.; Gommers, R.; Oliphant, T.E.; Haberland, M.; Reddy, T.; Cournapeau, D.; Burovski, E.; Peterson, P.; Weckesser, W.; Bright, J.; et al. SciPy 1.0: Fundamental algorithms for scientific computing in Python. Nat. Methods
**2020**, 17, 261–272. [Google Scholar] [CrossRef] [PubMed][Green Version] - Ortner, M.; Bandeira, L.G.C. Magpylib: A free Python package for magnetic field computation. SoftwareX
**2020**, 11, 100466. [Google Scholar] [CrossRef] - Higham, N.J. Accuracy and Stability of Numerical Algorithms; SIAM: Philadelphia, PA, USA, 2002. [Google Scholar]
- Jackson, J.D. Classical electrodynamics. Am. J. Phys.
**1999**, 67, 841. [Google Scholar] [CrossRef] - Ortner, M.; Filipitsch, B. Feedback of Eddy Currents in Layered Materials for Magnetic Speed Sensing. IEEE Trans. Magn.
**2017**, 53, 1–11. [Google Scholar] [CrossRef] - Simpson, J.C.; Lane, J.E.; Immer, C.D.; Youngquist, R.C. Simple Analytic Expressions for the Magnetic Field of a Circular Current Loop; Technical Report; NASA, Kennedy Space Center: Merritt Island, FL, USA, 2001. [Google Scholar]
- Behtouei, M.; Faillace, L.; Spataro, B.; Variola, A.; Migliorati, M. A novel exact analytical expression for the magnetic field of a solenoid. Waves Random Complex Media
**2020**, 32, 1977–1991. [Google Scholar] [CrossRef] - González, M.A.; Cárdenas, D.E. Analytical Expressions for the Magnetic Field Generated by a Circular Arc Filament Carrying a Direct Current. IEEE Access
**2020**, 9, 7483–7495. [Google Scholar] [CrossRef] - Prantner, M.; Parspour, N. Analytic multi Taylor approximation (MTA) for the magnetic field of a filamentary circular current loop. J. Magn. Magn. Mater.
**2021**, 517, 167365. [Google Scholar] [CrossRef] - Chapman, G.H.; Carleton, D.E.; Sahota, D.G. Current Loop Off Axis Field Approximations with Excellent Accuracy and Low Computational Cost. IEEE Trans. Magn.
**2022**, 58, 1–6. [Google Scholar] [CrossRef] - Seleznyova, K.; Strugatsky, M.; Kliava, J. Modelling the magnetic dipole. Eur. J. Phys.
**2016**, 37, 025203. [Google Scholar] [CrossRef] - Schill, R.A. General relation for the vector magnetic field of a circular current loop: A closer look. IEEE Trans. Magn.
**2003**, 39, 961–967. [Google Scholar] [CrossRef] - Urzhumov, Y.; Smith, D.R. Metamaterial-enhanced coupling between magnetic dipoles for efficient wireless power transfer. Phys. Rev. B
**2011**, 83, 205114. [Google Scholar] [CrossRef] - Rong, Z.; Wei, Y.; Klinger, L.; Yamauchi, M.; Xu, W.; Kong, D.; Cui, J.; Shen, C.; Yang, Y.; Zhu, R.; et al. A New Technique to Diagnose the Geomagnetic Field Based on a Single Circular Current Loop Model. J. Geophys. Res. Solid Earth
**2021**, 126, e2021JB022778. [Google Scholar] [CrossRef] - Alldredge, L.R. Circular current loops, magnetic dipoles and spherical harmonic analyses. J. Geomagn. Geoelectr.
**1980**, 32, 357–364. [Google Scholar] [CrossRef][Green Version] - Bulirsch, R. Numerical calculation of elliptic integrals and elliptic functions. III. Numer. Math.
**1969**, 13, 305–315. [Google Scholar] [CrossRef] - Derby, N.; Olbert, S. Cylindrical magnets and ideal solenoids. Am. J. Phys.
**2010**, 78, 229–235. [Google Scholar] [CrossRef][Green Version] - Fukushima, T.; Kopeikin, S. Elliptic functions and elliptic integrals for celestial mechanics and dynamical astronomy. Front. Relativ. Celest. Mech.
**2014**, 2, 189–228. [Google Scholar] - Fukushima, T.; Ishizaki, H. Numerical computation of incomplete elliptic integrals of a general form. Celest. Mech. Dyn. Astron.
**1994**, 59, 237–251. [Google Scholar] [CrossRef] - Caciagli, A.; Baars, R.J.; Philipse, A.P.; Kuipers, B.W. Exact expression for the magnetic field of a finite cylinder with arbitrary uniform magnetization. J. Magn. Magn. Mater.
**2018**, 456, 423–432. [Google Scholar] [CrossRef] - Slanovc, F.; Ortner, M.; Moridi, M.; Abert, C.; Suess, D. Full analytical solution for the magnetic field of uniformly magnetized cylinder tiles. J. Magn. Magn. Mater.
**2022**, 559, 169482. [Google Scholar] [CrossRef] - Press, W.H.; Teukolsky, S.A.; Vetterling, W.T.; Flannery, B.P. Numerical Recipes 3rd Edition: The Art of Scientific Computing; Cambridge University Press: Cambridge, UK, 2007. [Google Scholar]
- Reinsch, K.D.; Raab, W. Elliptic Integrals of the First and Second Kind—Comparison of Bulirsch’s and Carlson’s Algorithms for Numerical Calculation. In Special Functions; World Scientific: Singapore, 2000; pp. 293–308. [Google Scholar]

**Figure 1.**(

**a**) Sketch of a current loop and the observed first quadrant. (

**b**) The number of correct significant figures of a straightforward implementation of the textbook expression for the radial component of the B-field of a current loop on a log–log scale.

**Figure 2.**(

**a**) Sketch of typical current loop positioning with the package Magpylib. (

**b**) Demonstrating the relevance of the numerical instability, which becomes visible when a current loop rotates about an observer.

**Figure 3.**Relative error of a straightforward implementation of the textbook expressions for the B-field of a current loop. (

**a**) Radial and (

**b**) axial components, as well as (

**c**) vectorwise analysis reveal a high level of numerical instability. (

**d**) The relation between the cylindrical coordinates and the important quantity k.

**Figure 4.**(

**a**) Two different implementations of the function ${\xi}_{0}$ and (

**b**) the relative difference when comparing them to each other.

**Figure 5.**Vectorwise relative error of the dipole approximation (

**a**) and a Taylor approximation (

**b**) of the B-field of a current loop. The colored contour lines show the numerical error from a straightforward implementation of the classical textbook expressions.

**Figure 6.**Vectorwise relative error of Taylor approximations of the B-field of a current loop. (

**a**) Expansion for small k. (

**b**) Expansion for small q.

**Figure 8.**(

**a**) Computation times of various methods with respect to the fastest one (dipole) as a function of ${k}^{2}$. (

**b**) The respective vectorwise relative errors.

Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |

© 2022 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).

## Share and Cite

**MDPI and ACS Style**

Ortner, M.; Leitner, P.; Slanovc, F.
Numerically Stable and Computationally Efficient Expression for the Magnetic Field of a Current Loop. *Magnetism* **2023**, *3*, 11-31.
https://doi.org/10.3390/magnetism3010002

**AMA Style**

Ortner M, Leitner P, Slanovc F.
Numerically Stable and Computationally Efficient Expression for the Magnetic Field of a Current Loop. *Magnetism*. 2023; 3(1):11-31.
https://doi.org/10.3390/magnetism3010002

**Chicago/Turabian Style**

Ortner, Michael, Peter Leitner, and Florian Slanovc.
2023. "Numerically Stable and Computationally Efficient Expression for the Magnetic Field of a Current Loop" *Magnetism* 3, no. 1: 11-31.
https://doi.org/10.3390/magnetism3010002