MATLAB CODE
written by John Bardsley
(unless otherwise noted)
These codes come with no guarantees