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

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 Comments

Your index starts from 0.Index cannot be zero.
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.

Sign in to comment.

 Accepted Answer

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 Comments

Hi KSSV,
Thank you for the speedy responses. The only thing with this is that I am trying to plot specific values of "phi" from my U_Rad_Norm_dB. With this approach, it calculates all of the values for phi and theta for that range in the beginning. But it doesn't allow me to access those specific values of "phi" from the U_Rad_Norm_dB value.
Essentially, I'm trying to plot the following:
theta = 0:0.01:pi/2
phi_E_1 = 0;
phi_E_2 = pi;
phi_H_1 = pi/2;
phi_H_2 = 3*(pi/2);
U_Rad_Norm_dB(theta,phi_E_1)
U_Rad_Norm_dB(theta,phi_E_2)
U_Rad_Norm_dB(theta,phi_H_1)
U_Rad_Norm_dB(theta,phi_H_2)
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.
Thank you for this. This helped me get started and I was able to build on this.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 10 Jul 2024

Commented:

on 12 Jul 2024

Community Treasure Hunt

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

Start Hunting!