%% 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

%% Array Factor
d_theta_deg=1.;
for itheta=1:181
    theta_deg=(itheta-1)*d_theta_deg;
    theta=theta_deg*(pi/180.);

    arr_theta_deg(itheta)=theta_deg;

    % Array Factor
    af=0.;
    for i=1:n
        af=af+w(i)*exp(complex(0,1)*k0*(sin(theta)*(x(i)*cos(phi)+y(i)*sin(phi))+z(i)*cos(theta)));
    end
    arr_af(itheta)=af;
end

plot(arr_theta_deg,abs(arr_af))
plot(arr_theta_deg,20*log10(abs(arr_af)))