function s = yagi(p,l,d,db,sg)
% Yagi antenna with boom on the x-axis
% p  :  Vector of x-positions of the elements.
%       Element 1 is the feeded dipole
% l  :  Vector of length of the elements (in y-direction
% d  :  Vector of diameters of the elements.
%       If d is scalar all elements will have this diameter.
% db :  Diameter of the boom, if db=0 no boom
% sg :  if 14 segments per lambda are not sufficient
%       sg can be set to 2, 3, ... to simulate with 28, 41, ... segments
%
% The main radiation will be in positive or negative x-direction
% depending on p and l.
%
% yagi without parameters is a 7-element Yagi for 2 m.

% author:  Klaus von der Heide,  e-mail :  v.d.heide@on-line.de

% Defaults and Constants
% ======================
if nargin<5  sg = 2;     end  % 14 segments per wavelength
if nargin<4  db = 0;     end  % no boom
if nargin<3   d = 0.002; end  % element diameter = 2 mm
if nargin<1                   % 7-element Yagi for 144 MHz
   p =   [0.000 -0.390  0.165  0.540  1.080  1.650  2.340] - 0.90;
   l = 2*[0.5000 0.5175 0.4775 0.4725 0.4700 0.4675 0.4550];     
end
if length(d)==1  d = d*ones(size(p)); end
m = 7*round(sg);              % number of segments per element

% Checking the input parameters
% =============================
[pm,pn] = size(p); [lm,ln] = size(l); [dm,dn] = size(d);
if pm~=1  error('yagi : parameter p has wrong dimension'); end
if lm~=1  error('yagi : parameter l has wrong dimension'); end
if dm~=1  error('yagi : parameter d has wrong dimension'); end
if pn~=ln | pn~=dn  error('yagi : length of parameters p, l, d incompatible'); end

% Building the structure
% ======================
lh = l/2;
a  = [p; -lh; zeros(1,pn)]';  % coordinates of one end of elements
b  = [p; +lh; zeros(1,pn)]';  % coordinates of the other end of elements
dp = exwire(a(1,:),b(1,:),d(1),m,1);           % dipole
el = wires(a(2:end,:),b(2:end,:),d(2:end),m);  % passive elements
if db>0
   boom = wire([min(p)-0.03 0 0], [max(p)+0.03 0 0], 0.01, 20); % Boom
   s    = comment(construct(dp,el,boom),[num2str(length(p)) '-Element-Yagi']);
else
   s    = comment(construct(dp,el),[num2str(length(p)) '-Element-Yagi']);
end
