% Script circloop
% 
% Calculates magnetic field in the plane of a circular current loop, as a
% function of distance from the center of the loop, with npts resolution.
% Uses Biot-Savarts law, Eq. (4.106) i the textbook by N.N. Rao, 
% Everything is normalized to a loop of radius unity, so the field has an
% overall scale factor I/(c*a), where a is the loop radius.
% 
% Input: npts, the number of data points to compute field for

if ~exist('npts');  npts = 5;  end
delr  =  1./npts;
delfi =  pi./(3.*npts);
r     =  0:delr:(1-delr); % radial position
fi    =  0:delfi:(2*pi-delfi); % integration variable
[r, fi]  =  meshgrid (r, fi); % sets up matrix of argument values
R     =  sqrt (1 + r.^2 - 2. .* r .* cos(fi)); % distance 
sinth =  (1. - r.*cos(fi))./R; % angle in cross product
Bz    =  delfi .* sum (sinth./R.^2); % approximates integral by sum over fi
plot (r, Bz);
grid on
xlabel ('Distance r/a from center of loop')
ylabel ('Magnetic field strength in units of I/(c*a)')
title  ('Field in current loop of radius a')
return

