"Index exceeds the number of array elements"

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 Kommentar

Walter Roberson
Walter Roberson am 29 Jan. 2020
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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Matt J
Matt J am 29 Jan. 2020
Bearbeitet: Matt J am 29 Jan. 2020

0 Stimmen

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 Kommentare

Matt J
Matt J am 29 Jan. 2020
Bearbeitet: Matt J am 29 Jan. 2020
Or perhaps this,
function [Ex, Ey, Ez] = E_field()
R_s = 0.02;
V = 0;
epnaut = 8.854187E-12;
k = 1/(4*pi*epnaut);
Q = (V*R_s)/k;
Ex = @(x,y,z) x*(k*Q/norm([x,y,z])^3) ;
Ey = @(x,y,z) y*(k*Q/norm([x,y,z])^3) ;
Ez = @(x,y,z) z*(k*Q/norm([x,y,z])^3) ;
end
Tom Keaton
Tom Keaton am 29 Jan. 2020
Why would using matlabFunction() not work here?
Also I am currently referencing this in another function as such. If I used this syntax, how would that change in the reference?
function bdip = bdipuniodefun(t,s)
%Using SI units
q = -1.60217662E-19;
m_e = 9.11E-31;
global Bx By Bz Ex Ey Ez
if isempty(Bx)
[Bx, By, Bz] = B_test();
end
if isempty(Ex)
[Ex, Ey, Ez] = E_test();
end
bdip = [s(4); s(5); s(6); (q/m_e)*(Ex(s(1),s(2),s(3)) + s(5)*Bz(s(1),s(2),s(3)) - s(6)*By(s(1),s(2),s(3))); (q/m_e)*(Ey(s(1),s(2),s(3)) + s(6)*Bx(s(1),s(2),s(3)) - s(4)*Bz(s(1),s(2),s(3))); (q/m_e)*(Ez(s(1),s(2),s(3)) + s(4)*By(s(1),s(2),s(3)) - s(5)*Bx(s(1),s(2),s(3)))];
end
Walter Roberson
Walter Roberson am 29 Jan. 2020
Your norm is 0. Multiply by x y z and you get 0 0 0. When you matlabFunction that without a vars option then matlab sees that the symvar([0 0 0]) is empty and decides that what is wanted is a function handle that takes no parameters and returns a 0 of constant size. If you provided a vars option you at least would get back a function handle that permitted parameters when it returned the scalar 0.0
@Matt J I get a new but the same error when I try using either of your recommendations:
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));
Error in bdipuniodefun
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in collisions7 (line 216)
[T,S] = ode15s(@bdipuniodefun, tspan, icv); %Matlab's ODE solver
Could you remind us which MATLAB version are you using?
MATLAB Version: 9.6.0.1099231 (R2019a) Update 1
Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 18363)
Java Version: Java 1.8.0_181-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
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 am 4 Feb. 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 1 Feb. 2020

0 Stimmen

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 Kommentar

Tom Keaton
Tom Keaton am 4 Feb. 2020
Under this specific circumstance this does work for me now.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 29 Jan. 2020

Kommentiert:

am 4 Feb. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by