Index exceeds number of array elements. Index must not exceed one. Error when applying length() outside of any loop

7 次查看(过去 30 天)
Hello everyone, there is some kind of bug in my code and I can't seem to find out what it is. I'm trying to run my code attached below, and for some reason I can't run the loop between lines 227 and 231. I got the index out of range error so I tried to obtain the length of the array "electrons_recoil" in order to index properly. However, to my surprise when I call the length function it outputs the error below, which I have never seen before. I tried transposing the array and using different formulations for length() unsuccesfully. Why is this happening? Workspace says the array has 296,132 entries as you can see in the attached screenshot. Thank you in advance. The line giving me the error is almost at the bottom on top of the loop I was trying to run initially.
CODE (relevant to question):
% Calculation of parameters to be initialized into emission model
% Stochastic calculation of electronic excitation from primary ion
clear
clc
% This code takes a series of inputs from TRIM and outputs positions, energies, and deflection angles of every electron that
% interacts with the main travelling ion
% Import the parsed data:
data = readmatrix("C:\Users\Jorge\Desktop\Uni\Research\JANUS\IIEE\Results\C0.5.txt"); % Reads .txt from specified directory
% Create relevant lists:
N = data(:,1);
x = data(:,2); % Depth entries (x-direction in A)
y = data(:,3); % y-direction (A)
z = data(:,4); % z-direction (A)
elec_stopping = data(:,5); % de_dx_e (eV/A)
% The following part of the code uses the refined data obtained above to perform the transmission analysis
% Extra input parameters are needed:
% Target - dependent properties
N_A = 6.022*10^23; % Avogadro's constant (mol^-1)
Z = 4; % Number of free electrons per atom
p_m = 1.93*10^-23; % Target density (g*A^-3)
m_a = 183.84; % Atomic mass per mole (g/mol)
% Projectile dependent properties
radii = 2.16; % Van der Waals radii of projectile (A)
% With input parameters obtain the following material-dependent quantities:
n_0 = (N_A*Z*p_m)/m_a; % Free electron density (electrons/A^3)
r = pi * radii^2; % Interaction area (A^2)
% Now calculate the distance and total energy transmitted to electrons the following way:
distance = [];
de_dx_e = [];
for k=1:length(x)-1
if N(k) == N(k+1)
distance(k) = sqrt((x(k+1)-x(k))^2+(y(k+1)-y(k))^2+(z(k+1)-z(k))^2);
end
end
for k=1:length(x)-1
if N(k) == N(k+1)
de_dx_e(k) = (elec_stopping(k)+elec_stopping(k+1))/2;
end
end
% Now the energy transferred to electrons per interval is:
Energy = distance.*de_dx_e;
% Next, calculate the interaction volume for each entry:
Volume = distance.*r;
% And the number of electrons that receive energy between collisions
electrons = Volume.*n_0;
electrons = round(electrons);
% Now, the energy transferred to each electron on the interval is:
E_electron = Energy./electrons;
% We need to create a list with energy values for each individual electron
Electron_energy = [];
for i = 1:length(electrons)
for j = 1:electrons(i)
Electron_energy(end+1) = E_electron(i);
end
end
% Now generate a list of random entries for depth x for each electron in
% each interval:
% First, generate the depth interval associated to each distance between
% collisions:
Electron_depth = zeros(1, sum(electrons)); % preallocate with a fixed length
Electron_angle = zeros(1, sum(electrons)); % preallocate with a fixed length
index = 1; % index for filling in the preallocated arrays
for i = 1:length(electrons)
for j = 1:electrons(i)
rand_val = x(i) + (x(i+1) - x(i))*rand(); % generate random value within range
rand_angle = rand()*360;
Electron_angle(index) = rand_angle;
Electron_depth(index) = rand_val; % add random value to preallocated arrays
index = index + 1; % increment the index
end
end
% Now for electron number
N_e = 1:length(Electron_energy);
% The final form for our electron information is:
Electron_info = zeros(length(N_e),4);
Electron_info(:,1) = N_e;
Electron_info(:,2) = Electron_energy;
Electron_info(:,3) = Electron_depth;
Electron_info(:,4) = Electron_angle;
% Stochastic calculation of electronic excitation from recoils
% This code takes a series of inputs from TRIM and outputs positions, energies, and deflection angles of every electron that
% interacts with recoiling atoms
% Now we are interested in obtaining the mean distance travelled by recoils
% for an energy interval
% Import the parsed data:
data_distance_recoils = readmatrix("C:\Users\Jorge\Desktop\Uni\Research\JANUS\IIEE\Results\CR0.5.txt"); % Reads .txt from specified directory
% Create relevant lists:
N_dist = data_distance_recoils(:,1);
x_dist = data_distance_recoils(:,2); % Depth entries (x-direction in A)
y_dist = data_distance_recoils(:,3); % y-direction (A)
z_dist = data_distance_recoils(:,4); % z-direction (A)
% Now calculate the distance and total energy transmitted to electrons the following way:
distance_recoils = [];
for k=1:length(x_dist)-1
if N_dist(k) == N_dist(k+1)
distance_recoils(k) = sqrt((x_dist(k+1)-x_dist(k))^2+(y_dist(k+1)-y_dist(k))^2+(z_dist(k+1)-z_dist(k))^2);
end
end
distance_recoils = nonzeros(distance_recoils);
magnitude_distance_recoils = mean(distance_recoils);
% Data left regarding recoils and operations:
% Import the parsed data:
data_recoil = readmatrix("C:\Users\Jorge\Desktop\Uni\Research\JANUS\IIEE\Results\R0.5.txt"); % Reads .txt from specified directory
% Create relevant lists:
N_recoil = data_recoil(:,1);
x_recoil = data_recoil(:,2); % Depth entries (x-direction in A)
y_recoil = data_recoil(:,3); % y-direction (A)
z_recoil = data_recoil(:,4); % z-direction (A)
E_recoil = data_recoil(:,5); % Energy of the recoil at xyz
% First step is to use the equation obtained from TRIM relating energy to
% stopping power in order to convert E_recoil into de_dx_e_recoil
% TRIM SRT relates the following values of tungsten energy and electronic
% stopping power in the following way:
E_fit_w = [25,27.5,30,32.5,35,37.5,40,45,50,55,60,65,70,80,90,100,110,120,130,140,150,160,170,180,200,225,250,275,300,325,350,375,400,450,500,550,600,650,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,2000,2250,2500,2750,3000,3250,3500,3750,4000,4500,5000];
elec_fit_w = [1.084,1.137,1.187,1.236,1.282,1.327,1.371,1.454,1.533,1.607,1.679,1.747,1.813,1.939,2.056,2.167,2.273,2.374,2.471,2.565,2.655,2.742,2.826,2.908,3.065,3.251,3.427,3.594,3.754,3.907,4.055,4.197,4.335,4.598,4.847,5.083,5.309,5.526,5.734,6.130,6.502,6.854,7.189,7.508,7.815,8.110,8.394,8.670,8.937,9.196,9.693,10.28,10.84,11.37,11.87,12.36,12.82,13.27,13.71,14.54,15.33];
% Similarly, using SRT we can fit the Backscatter.txt energy data in order
% to obtain an equation that converts from E to (dE/dx)e
E_fit_xe_w = [30,32.5,35,37.5,40,45,50,55,60,65,70,80,90,100,110,120,130,140,150,160,170,180,200,225,250,275,300,325,350,375,400,450,500,550,600,650,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,2000,2250,2500,2750,3000,3250,3500,3750,4000,4500,5000];
elec_fit_xe_w = [8.117E-01,8.448E-01,8.767E-01,9.075E-01,9.373E-01,9.941E-01,1.048,1.099,1.148,1.195,1.240,1.325,1.406,1.482,1.554,1.623,1.690,1.753,1.815,1.875,1.932,1.988,2.096,2.223,2.343,2.458,2.567,2.672,2.772,2.870,2.964,3.144,3.314,3.475,3.630,3.778,3.921,4.192,4.446,4.686,4.915,5.134,5.343,5.545,5.739,5.928,6.110,6.287,6.627,7.029,7.410,7.771,8.117,8.448,8.767,9.075,9.373,9.941,10.48];
% plot(E_fit_w,elec_fit_w) % If you plot this, a quadratic fit relating E and
% dE_dx_e can be obtained
% For tungsten on tungsten this equation is (for 25-5000 eV):
de_dx_e_recoil = -8.11e-14.*E_recoil.^4+9.333e-10.*E_recoil.^3-3.845e-06.*E_recoil.^2 + 0.008808.*E_recoil + 1.228; % MIGHT HAVE TO CHANGE THE FIT FOR BROADER ENERGY RANGES!!!!!!!!!!!!
% For xenon on tungsten this equation is (for 25-5000 eV) (approximating with a 4th degree polinomial):
de_dx_e_fit_xe_w = -5.294e-14*E_fit_xe_w.^4 + 6.117E-10*E_fit_xe_w.^3-2.539E-06*E_fit_xe_w.^2+0.005915*E_fit_xe_w+0.8709;
% In a similar fashion as before, define the VdW radii of interaction for
% the recoiling atom
radii_recoil = 2.1; % Van der Waals radii of recoiling atom (A)
% With input parameters obtain the following material-dependent quantities:
n_0 = (N_A*Z*p_m)/m_a; % Free electron density (electrons/A^3)
r_recoil = pi * radii_recoil^2; % Interaction area (A^2)
% Now calculate the distance and total energy transmitted to electrons the following way:
% Now the energy transferred to electrons per interval is:
Energy_recoil = magnitude_distance_recoils.*de_dx_e_recoil;
length = length(Energy_recoil);
% Next, calculate the interaction volume for each entry:
Volume_recoil = magnitude_distance_recoils.*r_recoil;
Volume_recoil = Volume_recoil*ones(length,1);
% And the number of electrons that receive energy between collisions
electrons_recoil = Volume_recoil.*n_0;
electrons_recoil = round(electrons_recoil);
% Now, the energy transferred to each electron on the interval is:
E_electron_recoil = Energy_recoil./electrons_recoil;
a = length(electrons_recoil)
% We need to create a list with energy values for each individual electron
Electron_energy_recoil = [];
for i = 1:range(electrons_recoil)
for j = 1:electrons_recoil(i)
Electron_energy_recoil(end+1) = E_electron_recoil(i);
end
end
% Now generate a list of random entries for depth x for each electron in
% each interval:
% Find indices of NaN values
nan_indices = isnan(electrons_recoil);
% Remove NaN values using logical indexing
electrons_recoil = electrons_recoil(~nan_indices);

采纳的回答

KSSV
KSSV 2023-5-2
It looks like you have used the functions length as a variable name. The work space shows length as 296132.
You should not name a variable on a inbuilt function. Clear the variable and try.
Also to find out whether you have used length as a variable name use:
which length
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by