How to do correction in this piece of code?

1 visualizzazione (ultimi 30 giorni)
Sadiq Akbar
Sadiq Akbar il 1 Mag 2024
Commentato: Raj il 6 Mag 2024
clear;clc
u=[30 50 60 80];% [Theta1 Theta2 Phi1 Phi2] four angles
M=length(u);
P=M/2; % No. of sources
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);
d_circular=l/2;% spacing b/w antennas
circumference = N*d_circular;
a=circumference/(2*pi);% radius
% AF = sum(exp(-i*k*a*sin(theta(m))*(cos(phi(p)-phi_n))));
% x = abs(AF(m,p))*sin(theta(m))*cos(phi(p));
% y = abs(AF(m,p))*sin(theta(m))*sin(phi(p));
% z = abs(AF(m,p))*cos(theta(m));
% loops method:
for sourceNo=1:P
for m=0:N-1
AF(m+1,sourceNo) = exp(-1i*k*a*sind(u(1:2)-phi_n(m+1)));
x(m+1,sourceNo) = abs (AF(m+1,sourceNo))*sin(u(1:2))*cos(u(3:4));
y(m+1,sourceNo) = abs(AF(m+1,sourceNo))*sin(u(1:2))*sin(u(3:4));
z(m+1,sourceNo) = abs(AF(m+1,sourceNo))*cos(u(1:2));
end
end
AF
x
y
z
  1 Commento
Sadiq Akbar
Sadiq Akbar il 1 Mag 2024
Modificato: Sadiq Akbar il 1 Mag 2024
When I change this code as below, then it works but when I uncomment the x,y and z statements, it gives error.
%for sourceNo=1:P
for m=0:N-1
AF(m+1,1:P) = exp(-1i*k*a*sind(u(1:2)-phi_n(m+1)));
% x(m+1,1:P) = abs (AF(m+1,sourceNo))*sin(u(1:2))*cos(u(3:4));
% y(m+1,1:P) = abs(AF(m+1,sourceNo))*sin(u(1:2))*sin(u(3:4));
% z(m+1,1:P) = abs(AF(m+1,sourceNo))*cos(u(1:2));
end
%end

Accedi per commentare.

Risposte (1)

Raj
Raj il 1 Mag 2024
As per my understanding, I see in the following lines of code
for sourceNo=1:P
for m=0:N-1
AF(m+1,sourceNo) = exp(-1i*k*a*sind(u(1:2)-phi_n(m+1)));
When the compiler first runs, sourceNo takes value 1 (i.e. sourceNo=1)
AF(m+1, sourceNo) will refer to the the value in (m+1)th row and 1st column (since sourceNo=1). Now if you check the value in the right hand side of the equation, it is a 1x2 vector.
exp(-1i*k*a*sind(u(1:2)-phi_n(m+1)))
This throws the error stating "Unable to perform assignment because the indices on the left side are not compatible with the size
of the right side".
Once you make the following change in code-
%for sourceNo=1:P
for m=0:N-1
AF(m+1,1:P) = exp(-1i*k*a*sind(u(1:2)-phi_n(m+1)));
This makes AF(m+1, 1:P) a 1x2 vector, since P=2. This will not throw the error because now both LHS and RHS have compatible indices.
I hope this solves your query!
  2 Commenti
Sadiq Akbar
Sadiq Akbar il 1 Mag 2024
Thanks a lot for yor kind response. Yes, you are right but I have already given that above your reply.
Raj
Raj il 6 Mag 2024
I see the error - 'Incorrect dimensions for matrix multiplication' coming up if you uncomment x,y,z. The compiler throws this error when matrix are not fit for muliplication. Using the ' .* ' instead of ' * ' fixes the error.
x(m+1,1:P) = abs (AF(m+1,1:P)).*(sin(u(1:2)).*cos(u(3:4)));
y(m+1,1:P) = abs(AF(m+1,1:P)).*(sin(u(1:2)).*sin(u(3:4)));
z(m+1,1:P) = abs(AF(m+1,1:P)).*cos(u(1:2));
For better understanding on ' .* ' you an refer to the following documentation link-

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by