Index exceeds matrix dimensions error message

1 view (last 30 days)
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

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 13 Nov 2019
Edited: KALYAN ACHARJYA on 13 Nov 2019
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 Comments
Baxter Hughes
Baxter Hughes on 13 Nov 2019
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 on 13 Nov 2019
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.

Sign in to comment.

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!