Index in position 1 is invalid. Array indices must be positive integers or logical values.

조회 수: 4 (최근 30일)
Hello MathWorks Community,
I am running into an issue where I am trying to plot a function that has two variables.
I attached my code in the description:
E_Plane_H_Plane.m
I calculate my U_Rad_Norm_dB and it is in terms of theta and phi.
My theta variable is an array of values:
theta = 0:0.01:pi/2
The phi variable has "fixed" values
phi_E_1 = 0;
phi_E_2 = pi;
phi_H_1 = pi/2;
phi_H_2 = 3*(pi/2);
What I am trying to plot is:
1) E_Plane_1 = U_Rad_Norm_dB(theta,phi_E_1);
2) E_Plane_2 = U_Rad_Norm_dB(theta,phi_E_2);
3) H_Plane_1 = U_Rad_Norm_dB(theta,phi_H_1);
4) H_Plane_2 = U_Rad_Norm_dB(theta,phi_H_2);
My issue is that I calculate U_Rad_Norm_dB through several different variables. I'm not sure how to change it into a "function" to where I can "plug in" my "theta array" and my individual "phi" values.
When I try to use a for loop, it gives me the error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
I've tried to create a function previously for this, but it hasn't worked and it made things more difficult when I was trying to pass my (theta,phi) values.
  댓글 수: 2
Ammar
Ammar 2024년 7월 10일
Hi KSSV, Thank you for the quick response. When I put my loop variable "n" starting from n =0, it gives this error:
Array indices must be positive integers or logical values.

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2024년 7월 10일
You need to proceed like this:
tic
% Specifying the length of the array
N = 1000;
% Making phi and theta array lengths the same size
phi = linspace(0,2*pi,N);
theta = linspace(0,pi/2,N);
[phi,theta] = meshgrid(phi,theta) ;
% Permittivity Value
e_r = 5;
% Permeability Value
u_r = 1;
% Replacing "j" with "1i" for robustness
j = 1i;
% Intrinsic Impedance of Free Space
Eta_Air = 377;
% Speed of light in meters/second = c = 3 * 10^8 m/s
% Speed of light in centimeters/second = c = 3*10^10 cm/s
c = 3*10^8;
% Operational Frequency in GHz = 3 GHz
% Operational Frequency in Hz = 3 * 10^9 Hz
f_oper = 3*10^9;
% Operational Wavelength in cm = Speed of light/Operational Frequency
lambda_oper = c / f_oper;
% Wavenumber in free space
k = (2*pi)/(lambda_oper);
% Length of the patch in cm
L = (lambda_oper)/(2*sqrt(e_r * u_r));
% Width of the Patch in cm
W = 0.75*L;
% Thickness of the substrate in meters
d = ((lambda_oper)/(20*sqrt(e_r*u_r)));
% Value of r = 1, because "r" cancels out
r=1;
% Stationary expressions
k_x = (k).*sin(theta).*cos(phi);
k_y = (k).*sin(theta).*sin(phi);
k_z_1 = (k).*sqrt((e_r*u_r)-(sin(theta)).^2);
k_z_2 = (k).*cos(theta);
% Msx
Ms_x = -d*2*(L^2)*k_x.*sin((k_y*W)/2).*exp((j*k_x*L)/2).*exp((j*k_y*W)/2).*(cos((k_x*L)/2).*(1./((pi)^2 - (k_x*L).^2)));
% Msy
Ms_y = (-d*2*k_x.*cos((k_x*L)/2).*exp((j*k_x*L)/2).*exp((j*k_y*W)/2).*W.*(sin((k_y*W)/2).*((k_y*W)/2))).^2;
% Tm and Te
Tm = (e_r)*k_z_2.*cos(k_z_1*d)+(j.*k_z_1.*sin(k_z_1*d));
Te = k_z_1.*cos(k_z_1.*d)+(j.*k_z_2.*u_r.*sin(k_z_1*d));
% Calculating E_theta
E_theta_1 = j*k.*cos(theta).*(exp(-j*k*r))*(1/(2*pi*r)).*cos(k_z_1*d).*exp(j.*k_z_2*d);
E_theta_2 = (e_r).*k.*sin(phi).*(1./Tm).*Ms_x;
E_theta_3 = (e_r)*k.*cos(phi).*(1./Tm).*Ms_y;
E_theta = (E_theta_1).*((E_theta_2 - E_theta_3));
% Calculating E_Phi
E_phi_1 = j*k.*cos(theta).*(exp(-j*k*r))*(1/(2*pi*r)).*cos(k_z_1*d).*exp(j.*k_z_2*d);
E_phi_2 = k_z_1.*cos(phi).*(1./Te).*(Ms_x);
E_phi_3 = k_z_1.*sin(phi).*(1./Te).*(Ms_y);
E_phi = (E_phi_1).*(E_phi_2 + E_phi_3);
% Magnitude of E_theta
E_theta_conjugate = conj(E_theta);
E_theta_total = E_theta.*E_theta_conjugate;
% Magnitude of E_phi
E_phi_conjugate = conj(E_phi);
E_phi_total = E_phi.*E_phi_conjugate;
% Taking the Real Part of E_theta and E_phi
E_Field_Total = real(E_theta_total+E_phi_total);
% Radiation Intensity (Linear)
U_Rad = (((r)^2)/(2*Eta_Air)).*E_Field_Total;
% Normalized Radiation Intensity (Linear)
U_Rad_Norm = U_Rad / max(U_Rad(:));
% Normalized Radiation Intensity (dB)
U_Rad_Norm_dB = 10*log10(U_Rad_Norm);
  댓글 수: 3
KSSV
KSSV 2024년 7월 10일
You can index with theta as it is a variable and has zero value. What you can do is, make your required theta and phi use interp2 to extract values at your required variables.
Ammar
Ammar 2024년 7월 12일
Thank you for this. This helped me get started and I was able to build on this.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Beamforming and Direction of Arrival Estimation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by