필터 지우기
필터 지우기

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

조회 수: 1 (최근 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에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by