File Exchange

image thumbnail

Magnetic Field Modeling

version 1.0.0 (21.3 KB) by Federico Masiero
Collections of scripts and functions for the generation of magnetic fields and for their graphical representations.


Updated 12 Jan 2020

From GitHub

View license on GitHub

Collection of functions for the generation the magnetic flux density of axially magnetized cylinders (equivalently of solenoids) and axially magnetized rings. The implemented model used for all the calculations can be found in the article:

[1] Derby, Norman, and Stanislaw Olbert. “Cylindrical Magnets and Ideal Solenoids.” American Journal of Physics 78.3 (2010): 229–235. Crossref. Web .

For an efficient computation of the field, you can find an implementation of the Bulirsch algorithm, which can compute in a fast way (according to a specified tolerance) a generalized complete elliptic integral, also known as Bulirsch's integral. Ref. [1] reports the algorithm in the Appendix, however you can also check:


for additional information about the relationships of the generalized complete elliptic integral (cel) with the complete elliptic integrals K (first kind), E (second kind) and Pi (third kind). In particular the developed function can be adopted for an efficient computation of the complete elliptic integrals, since it performs better than the original built-in Matlab function ellipke and ellipticPi.

Additionally this repository provides some fancy functions to draw the cylindrical permanent magnets and ring magnets with custom choice of position, orientation, size and appearance (pattern and color), and functions to draw vectors and reference frames (e.g. to show the magnetic moment vector of the magnets).

The code is provided with a lot of comments and some useful DEMOs which also provides the plot of the magnetic flux density (thanks to David CEBRON for the useful code of the plots and for his very nice documentation,

Cite As

Federico Masiero (2021). Magnetic Field Modeling (, GitHub. Retrieved .

Comments and Ratings (1)

Robert Siddall

MATLAB Release Compatibility
Created with R2019b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!