Index exceeds the number of array elements (101)

조회 수: 3 (최근 30일)
Jamie Al
Jamie Al 2021년 2월 27일
편집: Jamie Al 2021년 2월 27일
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
Walter Roberson
Walter Roberson 2021년 2월 27일
We do not have efield() so we cannot test.
Jamie Al
Jamie Al 2021년 2월 27일
Thanks Walter, I fixed the error, now the code is working.

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

답변 (1개)

Jan
Jan 2021년 2월 27일
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
  댓글 수: 3
Jamie Al
Jamie Al 2021년 2월 27일
Ok, I see in the workspace it says i =3, but I have no idea how is that possible? i in the WeakBeam main code goes from 1:numSteps which is 5000 and i in the function goes from 1 to x_p which is 100. Is it being overwritten somehow?
Jamie Al
Jamie Al 2021년 2월 27일
So I was able to fix it by changing rhob in the main code to just rho and same thing with rhoe :/

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by