Index exceeds the number of array elements (101)
Show older comments
I keep getting this error for a function I wrote. Not quiet sure what it really means since I think the sizes of xe and xb are correct. This function will take some array (charge density) and updates it using the "new" particle positions (xe, xb)
The error message:
Index exceeds the number of array elements (101).
Error in Rho (line 41)
rho(step_1) = rho(step_1) + q*(x(step) - x_p(i))/(dx^2); % rho(j-1)
Error in WeakBeam (line 150)
rhob = Rho(rhob,xb,qb,x,dx);
In weakBeam main code the line before rhob is rhoe and it seems to be working fine although xb and xe are the same sizes. So I am not sure why specifically line 41 is the problem?
I will be attaching the two files (WeakBeam) and function Rho as well
4 Comments
Mohammad Sami
on 27 Feb 2021
You are using step_1, step, and i as indexes for arrays rho,x,x_p. Look at the size of these arrays and values of the indexes used. One of the index is having a value larger then the number of elements in the array.
Jamie Al
on 27 Feb 2021
Walter Roberson
on 27 Feb 2021
We do not have efield() so we cannot test.
Jamie Al
on 27 Feb 2021
Answers (1)
Jan
on 27 Feb 2021
Use the debugger to find the not matichiung index. Type this in the command window:
dbstop if error
and run the code again. When Matlab stops, check the value of the indices:
% rho(step_1) = rho(step_1) + q*(x(step) - x_p(i))/(dx^2)
size(rho)
step_1
size(x)
step
size(x_p)
i
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!