I tried this code but it gives me an error

There are 8 antennas placed in a circle and there are two sources in the far field from which rays are coming and are incident on these antennas. The mathematical formula for array factor "AF" is given in the attachment. This derivation of this formula is also attached. Now if the angles of those two sources are 30 and 70 respectively which I assign to a vector u. Then I want to put these angles one by one while spanning all the 8 antennas. I mean when I put angle 30 in the formula, then the AF should be calculated over all the 8 antennas. Likewise then put the 2nd angle 70 in the formula and calculate the AF over all the 8 antennas. So when I check the AF, it should be an 8 x 2 matrix where 8 are antennas and 2 are angles. I have tried for its code as below but it gives me an error:
clear;clc
f=1e9;% frequency
c=3e8;% speed of light
l=c/f;% lambda
k=(2*pi)/l;% wavenumber
N=8;% Number of antennas
n=0:N-1;
phi_n=2*pi*n/N;
u=[30 70];% The two Angles
M=length(u);% Number of angles
d_circular=l/2;% spacing b/w elements
circumference = N*d_circular;
a = circumference/2*pi; % Radius
for sourceNo=1:M
for m=0:N-1
% AF = sgma from n=0 to N-1 (exp(-1j*k*a*cos(phi-phin))
AF(m)=sum(exp(-1i*k*a*(cos(u(sourceNo)-phi_n))));
end
end
AF

 Accepted Answer

u appears to be in degrees, but phi_n appears to be in radians. You need to convert one or the other, and then use the appropriate cosine function (cos or cosd). I'll convert u to radians and use cos.
If you want AF to be 8x2, you'll need two indices in the expression for AF, i.e., use m+1 as the row index (or change m to be 1:N and use m as the index) and use sourceNo as the column index.
Also, based on the comment "AF = sgma from n=0 to N-1 (exp(-1j*k*a*cos(phi-phin))", you should only use one element from phi_n in the calculation of each element of AF. If you sum at that point, the result is the same for each m (i.e., each row of AF). You can sum afterward if that's what you need to do.
f=1e9;% frequency
c=3e8;% speed of light
l=c/f;% lambda
k=(2*pi)/l;% wavenumber
N=8;% Number of antennas
n=0:N-1;
phi_n=2*pi*n/N;
u=[30 70];% The two Angles
u = deg2rad(u);
M=length(u);% Number of angles
d_circular=l/2;% spacing b/w elements
circumference = N*d_circular;
a = circumference/2*pi; % Radius
for sourceNo=1:M
for m=0:N-1
% AF = sgma from n=0 to N-1 (exp(-1j*k*a*cos(phi-phin))
AF(m+1,sourceNo)=exp(-1i*k*a*cos(u(sourceNo)-phi_n(m+1)));
end
end
size(AF)
ans = 1x2
8 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
AF
AF =
-0.9330 - 0.3599i 0.5930 - 0.8052i 0.9072 - 0.4206i -0.3417 + 0.9398i 0.6297 - 0.7769i 0.8245 + 0.5659i -0.7017 - 0.7125i -0.5601 + 0.8285i -0.9330 + 0.3599i 0.5930 + 0.8052i 0.9072 + 0.4206i -0.3417 - 0.9398i 0.6297 + 0.7769i 0.8245 - 0.5659i -0.7017 + 0.7125i -0.5601 - 0.8285i
sum(AF,1)
ans =
-0.1955 - 0.0000i 1.0314 + 0.0000i
Alternate expression (simpler, with no loops or indexing):
AF = exp(-1i*k*a*cos(u-phi_n.'))
AF =
-0.9330 - 0.3599i 0.5930 - 0.8052i 0.9072 - 0.4206i -0.3417 + 0.9398i 0.6297 - 0.7769i 0.8245 + 0.5659i -0.7017 - 0.7125i -0.5601 + 0.8285i -0.9330 + 0.3599i 0.5930 + 0.8052i 0.9072 + 0.4206i -0.3417 - 0.9398i 0.6297 + 0.7769i 0.8245 - 0.5659i -0.7017 + 0.7125i -0.5601 - 0.8285i
sum(AF,1)
ans =
-0.1955 - 0.0000i 1.0314 + 0.0000i

4 Comments

Thanks a lot for your kind response. Yes now its ok. But if (i) I want it in degrees, then how will I do it?
(ii) Further, if I take another vector b and compute AF with b and then I want to compute the error between both these AF and then I take the mse, how will I do it. I mean like the following:
clear;clc
f=1e9;% frequency
c=3e8;% speed of light
l=c/f;% lambda
k=(2*pi)/l;% wavenumber
N=8;% Number of antennas
n=0:N-1;
phi_n=2*pi*n/N;
u=[30 70];% The two Angles
b=u; % 2nd vector
u = deg2rad(u);
b = deg2rad(b);
M=length(u);% Number of angles
d_circular=l/2;% spacing b/w elements
circumference = N*d_circular;
a = circumference/2*pi; % Radius
for sourceNo=1:M
for m=0:N-1
% AF = sgma from n=0 to N-1 (exp(-1j*k*a*cos(phi-phin))
%AFu(m+1,sourceNo)=exp(-1i*k*a*cos(u(sourceNo)-phi_n(m+1)));
%AFb(m+1,sourceNo)=exp(-1i*k*a*cos(b(sourceNo)-phi_n(m+1)));
AFu(m+1,sourceNo)=exp(-1i*k*a*cosd(u(sourceNo)-phi_n(m+1)));
AFb(m+1,sourceNo)=exp(-1i*k*a*cosd(b(sourceNo)-phi_n(m+1)));
end
end
er=(abs(AFu-AFb)).^2;
er1=mean(er)
You're welcome! If this answer worked for you, please "Accept" it. Thanks!
(i) To use degrees, leave u as it was (in degrees), convert phi_n to degrees (e.g., using rad2deg), and use the cosd function to compute the cosines.
(ii) You basically have it already; you just need u, b, and phi_n in degrees.
f=1e9;% frequency
c=3e8;% speed of light
l=c/f;% lambda
k=(2*pi)/l;% wavenumber
N=8;% Number of antennas
n=0:N-1;
phi_n=2*pi*n/N;
phi_n = rad2deg(phi_n);
u=[30 70];% The two Angles
b=u; % 2nd vector
M=length(u);% Number of angles
d_circular=l/2;% spacing b/w elements
circumference = N*d_circular;
a = circumference/2*pi; % Radius
% loops method:
for sourceNo=1:M
for m=0:N-1
% AF = sgma from n=0 to N-1 (exp(-1j*k*a*cos(phi-phin))
AFu(m+1,sourceNo)=exp(-1i*k*a*cosd(u(sourceNo)-phi_n(m+1)));
AFb(m+1,sourceNo)=exp(-1i*k*a*cosd(b(sourceNo)-phi_n(m+1)));
end
end
% easier alternative:
AFu = exp(-1i*k*a*cosd(u-phi_n.'));
AFb = exp(-1i*k*a*cosd(b-phi_n.'));
er = abs(AFu-AFb).^2
er = 8x2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
er1 = mean(er,1) % mse of each source
er1 = 1x2
0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
er1 = mean(er,'all') % mse of all sources
er1 = 0
Of course the error is zero in this case because b==u.
Thanks a lot for your kind response. Yes, its ok now. Thanks once again.
You're welcome!

Sign in to comment.

More Answers (1)

u=[30 70];% The two Angles
Those look like degrees
AF(m)=sum(exp(-1i*k*a*(cos(u(sourceNo)-phi_n))));
But there you are treating them as radians.
You need cosd

1 Comment

Thanks a lot for your kind response. i replaced that by cosd but still it gives an error.

Sign in to comment.

Categories

Find more on Phased Array Design and Analysis in Help Center and File Exchange

Asked:

on 30 Apr 2024

Commented:

on 30 Apr 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!