MATLAB Code Archive

Electromagnetic field theory is a complex mathematical subject, and there are many different methods available when solving Maxwell's equations. Some physical systems have exact, closed-form solutions, like plane-wave reflection from a dielectric interface. Other systems have "near-exact" solutions, in which an infinite series solution is ultimately truncated to some reasonable approximation (e.g., plane wave scattering off a sphere). Most systems are so complex that a numerical solution is the only reasonable option (e.g., radar scattering off an F-16). Whatever the case may be, however, it takes hard work to generate efficient computer code that brings the solution to life.

This page contains an archive of useful electromagnetic codes written in MATLAB. It includes both analytic solvers and numerical solvers, plus many practical tools for calculating obscure mathematical functions and rendering the results.

The Beauty of Electromagnetics, by James Nagel

Useful Modeling Tools

@PHYSICAL_CONSTANTS Class definition for basic physical constants used throughout electromagnetism. Nearly every EM model will require these constants in some form, so it is convenient to store them as a single consolidated object.
@RectangularMesh Class definition for two-dimensional mesh object. Calculations with vector fields often involve plotting along two-dimensional cross-sections, and this code streamlines the process for better consistency.

 

Misc Graphical Tools

Download DrawArrow.m Renders a cartoon arrow onto the designated figure. The arrow can be customized for different sizes and styles.
Download DrawRectangle.m Renders a solid rectangle onto the designated figure.
DrawCircle.m Download DrawCircle.m Renders a solid circle onto the designated figure.
DrawRing.m Download DrawRing.m Renders a circular ring onto the designated figure.
DrawVectorField.m Download DrawVectorField.m Draws a bunch of arrows onto a figure to represent a vector field. The code includes a bunch of really handy features, like scaling, color, cutoff, etc.
ULogoVertices.m Download ULogoVertices.m Create an array of x- and y-coordinates that represent the vertices of the University of Utah's "U" logo. This is fun to have for any marketing/publicity renders.
@VectorField2D.ZIP Download @VectorField2D.ZIP Class definition, plus methods, for a vector field profile in 2D. It is similar in purpose to the DrawVectorField.m function above, but with more features. Use this to create beautiful renders of a vector field in 2D, as well as animations.

 

Transmission Lines & Microwave Network Solvers
Download ABCD2Smatrix.m Converts a microwave ABCD matrix into a scattering matrix.
Download Smatrix2ABCD.m Converts a 2-port microwave scattering matrix (S-matrix) into the corresponding ABCD matrix.
Download LSectionMatchingNetwork.m Calculate impedance/admittance for an L-section matching network.
Download SeriesStubMatchingNetwork.m Calculate distance/length for a series stub matching network.
Download ShuntStubMatchingNetwork.m Calculate distance/length for a shunt stub matching network.
TLineInputImpedance.m Download TLineInputImpedance.m Equivalent input impedance looking into a transmission line with some fixed load at the end
CoaxialCableParameters.m Download CoaxialCableParameters.m Calculate the transmission-line parameters (Z0,C0,L0,G0) for a coaxial cable.
@Telegrapher1DFDTD.zip Download @Telegrapher1DFDTD.zip Class definition, plus methods, for a finite-difference time-domain (FDTD) simulation of the Telegrapher's equations (i.e., transmission line). The ZIP file also contains a demonstration script (DemonstrateTransmissionLine1DFDTD.m) that shows how to use the code. For a full tutorial on FDTD with the Telegrapher's equations, see this PDF here Download here.

Fdtd_1D_TL_Rg58.m Download Fdtd_1D_TL_Rg58.m

Simplified FDTD simulation of the Telegrapher's equations (derivation here Download here). Includes RLGC transmission line model for RG58 coax, Voltage Generator Source (derivation here Download here), and Open, Short, Matched Boundaries. Stability Analysis is here Download here. See related ECE 3305 Lab: Here.

MicrostripElectricField.png

@Stripline.zip Download @Stripline.zip

Class definition, plus methods, for a stripline/microstrip transmission line model. Use this to calculate the scalar potential, charge density, and electric field profile around a stripline. The ZIP file also contains a demonstration script (RenderMicrostripElectricField.m) that sows how to use it. For a full tutorial on how the calculation works, see this PDF here Download here.
ProcessTDR.m Download ProcessTDR.m Compute the time-domain impulse response of a transmission line using Time-Domain Reflectometry (TDR). Input data consists of S11 measurements with a vector network analyzer (VNA).

https://youtu.be/iXMXbGNox3Q Links to an external site.

Download the code HERE (Links to an external site.).

 FDTD and FDM modeling codes for asymmetric faults in a multiconductor transmission line.

Hunter Ellis, Cody LaFlamme, James R. Nagel, Joel Harley, Cynthia M. Furse, "Multiconductor Transmission Lines and Asymmetric Fault in SSTDR Measurements," IEEE Sensors Journal, 2021 Download Hunter Ellis, Cody LaFlamme, James R. Nagel, Joel Harley, Cynthia M. Furse, "Multiconductor Transmission Lines and Asymmetric Fault in SSTDR Measurements," IEEE Sensors Journal, 2021

Download the code here Links to an external site.

Full system simulation for spread spectrum time domain reflectometry (SSTDR) on a transmission line, or system of sequential transmission lines/loads, using an adapted systematic solution procedure (SSP).

 

Electrostatic Field Solvers
UniformCylinderEField.m Download UniformCylinderEField.m Calculate the electric field intensity around a right circular cylinder of uniform charge density.
UniformCylinderScalarPotential.m Download UniformCylinderScalarPotential.m Calculate the electric scalar potential around a right circular cylinder of uniform charge density.
TwinMetalCylindersEField.m Download TwinMetalCylindersEField.m Electric field intensity around two symmetric metal cylinders with equal/opposite voltage.
TwinMetalCylindersScalarPotential.m Download TwinMetalCylindersScalarPotential.m Electric scalar potential around two symmetric metal cylinders with equal/opposite voltage.

U_Current_14x18.png

@Electrostatic2FDM.zip Download @Electrostatic2FDM.zip

Class definition, plus methods, for a two-dimensional electrostatic simulation using the finite-difference method (FDM). The ZIP file also contains a couple of demo scripts that validate the code and show beginners how to use it. For a comprehensive tutorial on how FDM works, see Nagel, 2014 Download Nagel, 2014.

 

Magnetostatic Field Solvers
UniformCylinderVectorPotential.m Download UniformCylinderVectorPotential.m Calculate the magnetic vector potential due to a cylinder of uniform current density.
UniformCylinderBField.m Download UniformCylinderBField.m  Calculate the magnetic field intensity due to a cylinder of uniform current density.

MagneticBarField_16x20.png

MagnetizedBarBField.m  Download MagnetizedBarBField.m 

Calculate the magnetic field intensity due to an infinite rectangular bar with permanent internal magnetization.
MagnetizedSphereBField.m Download MagnetizedSphereBField.m Calculate the magnetic field intensity due to a sphere with permanent internal magnetization along the z-axis.

 

Waveguide Solvers
@ParallelPlateWaveguide.ZIP Download @ParallelPlateWaveguide.ZIP Class definition, plus methods, for an ideal parallel-plate waveguide. The ZIP file also contains a demo script that allows the user freely toy with the model and observe/animate the resulting field profiles. A mathematical tutorial on the basic theory can be found here Download here.

 

Plane Waves, Radiation, Propagation, and Scattering
@Maxwell_1DFDTD.zip Download @Maxwell_1DFDTD.zip Class definition, plus methods, for a transient simulation of Maxwell's curl equations in one dimension using the finite-difference time-domain method (FDTD). The ZIP file also contains a handful of demo scripts that validate the code and show beginners how to use it. Download the tutorial here Download here for a mathematical derivation of FDTD in 1D using Maxwell's equations. I've also included tutorials on numerical dispersion Download numerical dispersion, radiation Download radiation, and the perfectly matched layer Download perfectly matched layer.

ScatteringDielectricCylinder.png 

@ScatteringCylinder Download @ScatteringCylinder

Class definition, plus methods, for a circular cylinder illuminated by a uniform plane wave. Both the cylinder plus ambient medium may have arbitrary material properties (permittivity, permeability, and conductivity). The methods can calculate all the expansion coefficients, plus all scattered and diffracted field profiles. For a tutorial on how the calculation works, see this PDF here Download see this PDF here. The ZIP file also contains a demo script that renders an animation of the total field profile.

FrustratedTIR.png

@OpticalStack Download @OpticalStack

Class definition, plus methods, for light propagation through a planar stratified medium (also known as an optical stack). Calculate the plane wave amplitudes and render the total field profile throughout the stack. The ZIP file also contains demo scripts to show how to use the class. For a mathematical derivation, see this tutorial here Download this tutorial here.

HertzianDipole14x18.png

@HertzianDipole.ZIP Download @HertzianDipole.ZIP

Class definition, plus methods, for the Hertzian electric dipole. Use this to calculate the radiated electric and magnetic field profiles. The dipole can be arbitrarily oriented or moved in space. The ZIP file includes two demonstration scripts that show how to generate field renderings. For a mathematical derivation of the field profiles, see this tutorial here Download this tutorial here.

lossy plane wave Download lossy plane wave

compute_emvalues.m Download compute_emvalues.m

Calculate attenuation and propagation constants, wavelength for a plane wave in lossy material

 .

Useful Mathematical Tools
SimpsonArea.m Download SimpsonArea.m Calculate the approximate area under a function by applying Simpson's rule.
CosCosInt.m Download CosCosInt.m Calculate the integral of cos(k1*x - p1)*cos(k2*x - p2) from [a,b].
SinSinInt.m Download SinSinInt.m Calculate the integral of sin(k1*x - p1)*cos(k2*x - p2) from [a,b].
srrcImpulseResponse.m Download srrcImpulseResponse.m Calculates the time-domain impulse response to a square-root-raised-cosine filter. The function is normalized so that convolution with itself results in a standard raised-cosine filter with unit amplitude in the time domain.
Polar2RectVectors.m Download Polar2RectVectors.m Converts a vector field from a polar unit-vector basis into rectangular.
bessel_dj.m Download bessel_dj.m Calculates the first derivative of the Bessel function of the first kind: J_nu(Z)
bessel_dh.m Download bessel_dh.m Calculates the first derivative of the Hankel function: H(NU,Z)
bessel_dy.m Download bessel_dy.m Calculates the first derivative of the Bessel function of the second kind: Y_nu(Z)

 

Utah Man Model and 3D FDTD solver (Fortran)

FDTD Code Archive 

 A collection of 1D, 2D, 3D FDTD codes
Utah Man Model  The Utah Man model is a 2x2x3mm MRI-derived man model with 31 tissue types.
Virtual Population Database Links to an external site. From It-IS Foundation.
Dielectric Database Links to an external site.

Detailed dielectric models of tissues and other dielectrics. From It-IS Foundation

 

Useful External Resources

PV PMC Matlab Toolbox Links to an external site.

PV_Lib Toolbox Links to an external site.

The PV_LIB Toolbox provides a set of well-documented functions for simulating the performance of photovoltaic energy systems. Currently there are two distinct versions (pvlib-python and PVILB for Matlab) that differ in both structure and content.   Both versions were initially developed at Sandia National Laboratories but have since been offered as open-source software projects and have grown significantly from contributions from an active community of users.
SEMCAD-X Links to an external site. Electromagnetic Modeling Software from SPEAG. Free Academic use.
Strata Links to an external site. Green's Functions for layered media