Asked by Tom Keaton
on 21 Aug 2019

Alright I managed to simplify this down to the most basic I can. I am getting an error in my ode set:

Index in position 1 is invalid. Array indices must be positive integers or logical values: .

Error in bdipuniodefun (line 10)

bdip = [s(4); s(5); s(6); (Ex(s(1),s(2),s(3))); (Ey(s(1),s(2),s(3))); (Ez(s(1),s(2),s(3)))];

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 EFieldTest (line 46)

[T,S] = ode15s(@bdipuniodefun, tspan, icv);

What does this mean? My code:

%TSTEP MUST BE ABLE TO GO INTO TFIN AN ODD AMOUNT OF TIMES (Because simulation includes tstep @ 0 so it ends up being even)

tstep = 1; %Defining time step

tfin = 11; %Defining final time

%Create zeros matrices to populate later

intspan = [0:tstep:tfin]; %Total time span

[introw,intcol] = size(intspan);

%Generate matrix that will be populated by positions and velocities (Each matrix within structure "posandvelmat" should be of size 2*introw-1 by 6)

posandvelmat = zeros(2*intcol-1,6);

spart(1) = 0;

spart(2) = 0;

spart(3) = 0;

vpart(1) = 0;

vpart(2) = 0;

vpart(3) = 0;

tic

for t = 0:1:intcol-2 %complete time interval and time step

%Defining relative position and velocity

x = spart(1);

y = spart(2);

z = spart(3);

vx = vpart(1);

vy = vpart(2);

vz = vpart(3);

%Coupled differential equation solver for trajectory within current time step

icv = [x; y; z; vx; vy; vz]; %Initial conditions

tspan = [intspan(t+1) ((intspan(t+2)-intspan(t+1))/2)+intspan(t+1) intspan(t+2)]; %Time span

%Trajectory solver

[T,S] = ode15s(@bdipuniodefun, tspan, icv);

posandvelmat((1+2*t):(3+2*t),(1:6)) = S;

%Redfine the velocity and position components to reference on next if-loop run

spart(1) = posandvelmat((3+2*t),1);

spart(2) = posandvelmat((3+2*t),2);

spart(3) = posandvelmat((3+2*t),3);

vpart(1) = posandvelmat((3+2*t),4);

vpart(2) = posandvelmat((3+2*t),5);

vpart(3) = posandvelmat((3+2*t),6);

end

bdipuniodefun script:

function bdip = bdipuniodefun(t,s)

persistent Ex Ey Ez

if isempty(Ex)

[Ex, Ey, Ez] = E_field_c();

end

bdip = [s(4); s(5); s(6); (Ex(s(1),s(2),s(3))); (Ey(s(1),s(2),s(3))); (Ez(s(1),s(2),s(3)))];

end

E_field_c script:

function [Ex, Ey, Ez] = E_field_c()

syms x y z

E = -2000.*z;

Ex = matlabFunction(E(1));

Ey = matlabFunction(E(2));

Ez = matlabFunction(E(3));

end

Ted Shultz
님의 답변 21 Aug 2019

Tom Keaton
21 Aug 2019

Ted Shultz
21 Aug 2019

0 is the not the inital condition, but an invalid index.

when you write

Ey(s(1),s(2),s(3))

Matlab things that you are saying that Ey is a three dimentional variable, and that you want the value definded by the location dim1 = s(1), dim2 = s(2) and dim3 = s(3).

I don't belive that is what you are trying to do. I think your notation could be tweaked.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Star Strider (view profile)

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/477070-ode-error-array-indices-must-be-positive-integers-or-logical-values#comment_737684

## Tom Keaton (view profile)

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/477070-ode-error-array-indices-must-be-positive-integers-or-logical-values#comment_737688

## Star Strider (view profile)

## 이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/477070-ode-error-array-indices-must-be-positive-integers-or-logical-values#comment_737699

Sign in to comment.