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?
0 件のコメント
回答 (1 件)
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
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
j
a(i)
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.
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!