%% Array Factor by T. Hirano

k0=2*pi;    % Wavenumber
phi_deg=0.;
phi=phi_deg*(pi/180.);

%% Weight and Positions of Each Element
n=6;
d=0.5;
for i=1:n
    w(i)=1; x(i)=0.; y(i)=0.; z(i)=(i-1)*d; 
end

r = @(u,v) abs(af(u,v,n,w,x,y,z));
funx = @(u,v) r(u,v).*cos(u).*sin(v);
funy = @(u,v) r(u,v).*sin(u).*sin(v);
funz = @(u,v) r(u,v).*cos(v);
fsurf(funx,funy,funz,[0 2*pi 0 pi]) 
camlight
%axis([-6 6 -6 6 -6 6])

function r = af(u,v,n,w,x,y,z)
    k0=2*pi;
    r=0.;
    for i=1:n
        r=r+w(i).*exp(complex(0,1).*k0.*(sin(v).*(x(i).*cos(u)+y(i).*sin(u))+z(i).*cos(v)));
    end
end
