1 view (last 30 days)
Show older comments
Marcos Granados Flores on 5 Mar 2021
Commented: Chris on 9 Mar 2021
Accepted Answer: Chris
Open in MATLAB Online
I want to create a movie with a for loop, but Index exceeds the number of array elements (1) is shown. Someone can tell me were is the error? The for loop without the condition works fine, but where i assign a condition, the error message appears. :( I just want that when y <= 0, the angle theta0 and v0 change to theta0 = atan(y(j-2)/(x(j)-x(j-2))) and v0 = 0.8v0.
clear, clc
g=9.81; theta0=45*pi/180; v0=5;
t(1)=0;x=0;y=0;
plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 8 0 0.8])
M(1)=getframe;
dt=1/128;
for j = 2:1000
t(j)=t(j-1)+dt;
x=v0*cos(theta0)*t(j);
y=v0*sin(theta0)*t(j)-0.5*g*t(j)^2;
plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8);
axis([0 8 0 0.8]);
M(j)=getframe;
if y<=0
theta0 = atan(y(j-2)/(x(j)-x(j-2)));
v0 = 0.8*v0;
plot(x(j),0,'o','MarkerFaceColor','b','MarkerSize',8)
M(j) = getframe;
end
end
pause
movie(M,1)
2 Comments Show NoneHide None
Show NoneHide None
Chris on 6 Mar 2021
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1372296
⋮
Open in MATLAB Online
I'm not entirely sure what value theta0 is meant to take when within that loop. y first turns negative when j=94, but don't forget that y is just a scalar the way you have it currently, so there's only one entry in position (1). In
theta0 = atan(y(j-2)/(x(j)-x(j-2)));
the first issue it encounters is y(j-2). At that point in the execution it becomes y(94-2) i.e. y(92) - the value of y in the 92nd column but y only has one entry/value, hence why you get that error.
Marcos Granados Flores on 6 Mar 2021
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1372391
Open in MATLAB Online
Oh, the model must be able to predict exactly when the ball hits the ground. At this point, the direction changes (the new angle will equal the negative of the angle at impact), that's why i put a conditional if y<=0. This is an excercise from Chapra´s Applied Methods, Chapter 3, excercise 3.21. I found the exercise made with a while loop, but I want to make it with a for loop if is possible. Here is the code
g = 9.81;
theta0 = 50*pi/180;
v0 = 5;
t(1) = 0;
x = 0;
y = 0;
plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 3 0 0.8])
M(1) = getframe;
dt = 1/128;
j=2;
x0=0;
t_last = 0;
h_stop = 0.01;
h_max = Inf;
while h_max>h_stop % para los saltos
flag = 0;
h_max = -1;
while(flag == 0)
t(j) = t(j - 1) + dt;
x(j) = v0*cos(theta0)*(t(j)-t_last)+x0;
y(j) = v0*sin(theta0)*(t(j)-t_last) - 0.5*g*(t(j)-t_last)^2;
if y(j)>h_max
h_max = y(j);
end
if y(j)<= 0
% Calcular el nuevo ángulo
theta0 = atan(y(j-2)/(x(j)-x(j-2)))
flag = 1;
v0 = 0.8*v0;
x0 = x(j);
t_last = t(j-1);
plot(x(j),0,'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 8 0 0.8])
M(j) = getframe;
else
plot(x(j),y(j),'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 8 0 0.8])
M(j) = getframe;
end
j=j+1;
end
end
movie(M,1)
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Chris on 6 Mar 2021
Open in MATLAB Online
Right, I see what you're trying to do but the point stands that your modified code doesn't save the different values of y as time increases, it only stores the current value, so going into y(j-2) finds nothing unless j=3. Keeping them as follows would address that:
x(j) = v0*cos(theta0)*(t(j)-t_last)+x0;
y(j) = v0*sin(theta0)*(t(j)-t_last) - 0.5*g*(t(j)-t_last)^2;
I would also say the use of while in this example is well suited to the conditions set, but if you want to replace them with for, that's also possible. As you've done, if you don't require watetight conditioning (i.e. just to run a couple of examples) you can set a high number of for loops (i.e. 1000/2000/etc), but then it's also wise to have a condition that checks if it's not needed to keep on looping and exit (i.e. when the ball no longer bounches).
g = 9.81;
theta0 = 50*pi/180;
v0 = 5;
t(1) = 0;
x = 0;
y = 0;
plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 3 0 0.8])
M(1) = getframe;
dt = 1/128;
x0=0;
t_last = 0;
h_max = -1;
for j=2:2000
t(j) = t(j - 1) + dt;
x(j) = v0*cos(theta0)*(t(j)-t_last)+x0;
y(j) = v0*sin(theta0)*(t(j)-t_last) - 0.5*g*(t(j)-t_last)^2;
if y(j)>h_max
h_max = y(j);
end
if y(j)<= 0
theta0 = atan(y(j-2)/(x(j)-x(j-2)));
flag = 1;
v0 = 0.8*v0;
x0 = x(j);
t_last = t(j-1);
plot(x(j),0,'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 8 0 0.8])
M(j) = getframe;
else
plot(x(j),y(j),'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 8 0 0.8])
M(j) = getframe;
end
if x(j)==x(j-1)
break
end
end
movie(M,1)
This is one way you can do it all with one for loop. The condition:
if x(j)==x(j-1)
break
end
checks if the object is no longer moving in the x direction and exits the loop if that's the case. So for the default parameters that's 332 steps. If you double the gravitational acceleration, it only needs 170 steps and exits on its own (no point looping for the remaining 1830.)
2 Comments Show NoneHide None
Show NoneHide None
Marcos Granados Flores on 9 Mar 2021
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1379287
Thank you so much!
Chris on 9 Mar 2021
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1379317
👍
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and ArraysCreating and Concatenating Matrices
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Tags
- for loop
Products
- MATLAB
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office