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 CenterFile Exchange에서 FSK에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by