Index exceeds matrix dimensions error message

2 ビュー (過去 30 日間)
Baxter Hughes
Baxter Hughes 2019 年 11 月 13 日
コメント済み: KALYAN ACHARJYA 2019 年 11 月 13 日
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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by