How to do correction in this piece of code?

1 回表示 (過去 30 日間)
Sadiq Akbar
Sadiq Akbar 2024 年 5 月 1 日
コメント済み: Raj 2024 年 5 月 6 日
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 件のコメント
Sadiq Akbar
Sadiq Akbar 2024 年 5 月 1 日
編集済み: Sadiq Akbar 2024 年 5 月 1 日
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

サインインしてコメントする。

回答 (1 件)

Raj
Raj 2024 年 5 月 1 日
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 件のコメント
Sadiq Akbar
Sadiq Akbar 2024 年 5 月 1 日
Thanks a lot for yor kind response. Yes, you are right but I have already given that above your reply.
Raj
Raj 2024 年 5 月 6 日
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-

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFSK についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by