Filter löschen
Filter löschen

Why am I receiving the error "Array indices must be positive integers or logical values."

3 Ansichten (letzte 30 Tage)
clc; clear;
%% Givens
dt=0.1;
x_0=0;
vx_0=90;
ax_0=10;
D=.03;
rho=1;
A=4;
m=20000;
n=1000;
t_0=0;
T=6000;
%% Initial Conditions
x=zeros(1,n);
x(1)=x_0;
vx=zeros(1,n);
vx(1)=vx_0;
ax=zeros(1,n);
vx(1)=vx_0;
t=zeros(1,n);
t(1)=t_0;
ax=zeros(1,n);
ax(1)=ax_0;
ax=(T/m)-((D*rho*A)/(2*m))/vx(i)^2;
for i=2:n
t(i)=t(i-1)+dt;
x(i)=x(i-1)+vx(i-1)*dt+0.5*ax(i-1)*dt^2;
vx(i)=vx(i-1)+(0.5*(ax(i-1)+ax(i))*dt);
end
Why am I receiving an error that says "Array indices must be positive integers or logical
values." What am I doing wrong?

Antworten (1)

Daniel Pollard
Daniel Pollard am 16 Dez. 2020
Line 26: Array indices must be positive integers or logical values. i is used as an index, but it isn't defined until a line later.
  2 Kommentare
Cris LaPierre
Cris LaPierre am 16 Dez. 2020
Bearbeitet: Cris LaPierre am 16 Dez. 2020
Well, i (as well as j) are special cases. They are defined as imaginary numbers by default, but are commonly overwritten by users as a loop counter. This is why you are not getting the "unknown variable or function error". It is generally a good idea to avoid using i or j for this reason.
a=1:5;
i
ans = 0.0000 + 1.0000i
j
ans = 0.0000 + 1.0000i
a(i)
Array indices must be positive integers or logical values.
Daniel Pollard
Daniel Pollard am 16 Dez. 2020
You're right. My default is to use ii, jj or k for loop indices for this reason, but I forgot when I replied to this user.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by