function [Brg,Bzg,Athetag]=bfield_circular_coil_analytic(a,d,r,z)
% Written by R. H. Goulding, 10/1/98
% Calculates the magnetic field components generated by a filamentary current
% loop. It is assumed that the loop is in a plane perpendicular to the z axis
% and is centered on the z axis. All units taken to be MKS (Amperes, meters,
% Tesla). Uses the Matlab function ellipke to calculate the complete elliptic
% integrals of the first and second kind K and E.
% Input parameters:
%
% a radius of current loop
% d axial distance of loop from z=0 point ( can be >0 or <0)%Icurrentinloop%rradiusatwhichBcomponentsarecalculated%zaxiallocationatwhichBcomponentsarecalculated%Outputparameters:%BrradialBcomponent%BzaxialBcomponent%Athetaazimuthalvectorpotential%J.D.LoremodifiedfromGouldingcnst=1.e-7;%mu0/(4*pi)u2=4*r.*a./((r+a).^2+(z-d).^2);%argumentofellipticalintegralfunctionsu=sqrt(u2);[K,E]=ellipke(u2);%Brg=-2*cnst./r.^(1.5).*(z-d)./sqrt(a).*(u/2.*K-u/4.*(u2-2)./(u2-1).*E);%Bzg=2*cnst./r.*(sqrt(r./a).*u/2.*K-u./(4*(u2-1)).*(sqrt(r./a).*(u2-2)+sqrt(a./r).*u2).*E);%Athetag=2*cnst./sqrt(r).*sqrt(a).*((2./u-u).*K-2./u.*E);sa = sqrt(a);sroa = sqrt(r./a);Brg=-2*cnst./r.^(1.5).*(z-d)./sa.*(u/2.*K-u/4.*(u2-2)./(u2-1).*E);Bzg=2*cnst./r.*(sroa.*u/2.*K-u./(4*(u2-1)).*(sroa.*(u2-2)+u2./sroa).*E);ifnargout> 2
Athetag=2*cnst./sroa.*((2./u-u).*K-2./u.*E);
end