function [Bx,By,Bz] = dipolex( x, y, z )

% [Bx,By,Bz] = dipolex( x, y, z )
% B = dipolex( x, y, z )

%    Bradley A. Moran 03/29/96
%    Copyright (C) 1996, Massachusetts Institute of Technology
%    Sea Grant Underwater Vehicles Laboratory
%    $Revision: 1.1 $    $Date: 2000/01/10 19:07:52 $

[xx,yy] = meshgrid( x, y );

if nargin < 3
    zz = zeros(size(xx));
else
    zz = z*ones(size(xx));
end

x2 = xx.^2;
y2 = yy.^2;
z2 = zz.^2;
denom = (x2 + y2 + z2).^(5/2);

Bx = (2*x2 - y2 - z2)./denom;
By = 3*xx.*yy./denom;
Bz = 3*xx.*zz./denom;

if nargout == 1
    Bx = sqrt( Bx.^2 + By.^2 + Bz.^2 );
end
