필터 지우기
필터 지우기

I am getting the following error for the below code: Subscript indices must either be real positive integers or logicals. How can i remove that error.

조회 수: 3 (최근 30일)
clc
clear all
g=9.81;
u=10;
t=0:.1:10;
y(1)=0;
while (y(t)>=0)
y(t)=(u*t)-(0.5*g*t.*t)
end
  댓글 수: 1
Sattik Basu
Sattik Basu 2017년 8월 19일
편집: Walter Roberson 2017년 8월 20일
i found a different solution to this:
g=9.81;
u=100;
t=0;
y=0;
while (y>=0)
disp(['at t=', num2str(t) ', the height is=', num2str(y)])
t=t+0.1;
y=(u*t)-(0.5*g*t^2);
end
why does this work and not the first one?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 8월 20일
You have
t=0:.1:10;
so t is a vector containing 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
You have
while (y(t)>=0)
but t is the vector discussed above, so your line is equivalent to asking
while y([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]) >= 0
However, this attempts to subscript y with 0 and non-integers.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by