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

3 次查看(过去 30 天)
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?

回答(1 个)

Daniel Pollard
Daniel Pollard 2020-12-16
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 个评论
Cris LaPierre
Cris LaPierre 2020-12-16
编辑:Cris LaPierre 2020-12-16
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 2020-12-16
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.

请先登录,再进行评论。

类别

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