MATLAB: 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.

while loop

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

Best Answer

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.