필터 지우기
필터 지우기

"Index exceeds the number of array elements"

조회 수: 1 (최근 30일)
Tom Keaton
Tom Keaton 2020년 1월 29일
댓글: Tom Keaton 2020년 2월 4일
Hello,
I am recieving this error:
Index exceeds the number of array elements (1).
Error in sym/subsref (line 890)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in E_field (line 7)
Ey = matlabFunction(E(2));
Here is the function file the error is referencing:
function [Ex, Ey, Ez] = E_field()
syms x y z
R_s = 0.02;
V = 0;
epnaut = 8.854187E-12;
k = 1/(4*pi*epnaut);
Q = (V*R_s)/k;
r = [x, y, z];
E = (k*Q/norm(r)^3)*r;
Ex = matlabFunction(E(1));
Ey = matlabFunction(E(2));
Ez = matlabFunction(E(3));
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 1월 29일
The posted code works for me. However you should using the 'vars' option of matlabFunction or else your function handle is not going to accept any arguments because your R values are all constants.

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

채택된 답변

Matt J
Matt J 2020년 1월 29일
편집: Matt J 2020년 1월 29일
Perhaps this is what you are looking for,
function [Ex, Ey, Ez] = E_field(x,y,z)
R_s = 0.02;
V = 0;
epnaut = 8.854187E-12;
k = 1/(4*pi*epnaut);
Q = (V*R_s)/k;
r = [x, y, z];
E = (k*Q/norm(r)^3)*r;
Ex = E(1);
Ey = E(2);
Ez = E(3);
end
  댓글 수: 9
Walter Roberson
Walter Roberson 2020년 2월 1일
Is there a reason you are using global instead of persistent ? Is there some other function that will use the created Bx By Bz Ex Ey Ez ?
Tom Keaton
Tom Keaton 2020년 2월 4일
Sorry I missed this question. The answer is yes which is why I am using it. I will show the code that causes this issue in another post.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 2월 1일
V = 0;
V is 0.
Q = (V*R_s)/k;
V is 0, so Q will be 0.
E = (k*Q/norm(r)^3)*r;
Provided that norm(r ) is not 0 and k is not infinity, then because Q is 0, E is going to be
zeros(size(r))
symvar() of a vector of 0 is empty. matlabFunction() of a constant, when you do not use any 'vars' option, is a function handle that accepts no inputs and returns a scalar copy of the constant.
The original code that was posted does not give me an index exceeded matrix dimension when I test it in R2019a.
  댓글 수: 1
Tom Keaton
Tom Keaton 2020년 2월 4일
Under this specific circumstance this does work for me now.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by