Index exceeds matrix dimensions error message

1 次查看(过去 30 天)
I'm trying to use a while loop to create vectors so that I can plot the values but I keep getting the "Index exceeds matrix dimensions" error message. Any help is appreciated. This is my code:
figure(1)
z1=.6;
z2=.85;
z3=1.23;
V=sqrt(2*9.81*(z3-z1));
i=1;
t=0;
%%
while z3(i) >=0
x(i)=t*V;
Z3(i)=z3-.5*9.81*(t^2);
t=t+.25;
i=i+1;
end

回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-11-13
编辑:KALYAN ACHARJYA 2019-11-13
Yoi defined z3 as scalar, but in the while loop you trying acess it as vectors
#Defined here
z3=1.23;
#
while z3(i)>=0
%...
end
Here waht does z3(i) means. In Matlab z3(i)
%.................................................................^ means array indexing
See the following example
>> Z3=4;
>> Z3(1)
ans =
4
>> Z3(2)
Index exceeds matrix dimensions.
>>
When "i" incremented within while loop, in the next iteration, how does z3 gets the value when i is greater than "i", like z3(2),z4(3)..so on.
Hope it helps, any issue let me know.
  2 个评论
Baxter Hughes
Baxter Hughes 2019-11-13
z3, z2, and z1 are all heights of 3 different jets of water and the while loops purpose is to determine the path of the jet over time. I used that expression for my while loop because i wanted the loop to run until Z3 got to zero.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019-11-13
You have to figure out, it seems simple
z1=.6;
z2=.85;
z3=1.23;
V=sqrt(2*9.81*(z3-z1));
i=1;
t=0;
while z3>=0
x(i)=t*V;
z3=z3-0.5*9.81*(t^2);
t=t+.25;
i=i+1;
end
Do the proper notbook calculation and show me, so that I can help you Matlab implementation.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by