Please help my for loop goes on forever (Matlab) :/

In summary, the question is asking for the minimum and maximum initial velocities for a ball to land in a pool that is 22m long and 9m away from a wall. The ball is launched from a height of 12m and the ramp is angled at 1 radian, with vx = 0.84v and vy = 0.54v at time zero. The ball should not be airborne for more than 3 seconds. A loop was attempted but was not successful due to the large number of iterations. A different approach may be needed for solving this problem.
  • #1
irNewton
17
0

Homework Statement



Question: A ball is launched from a height of 12m. It should land in a pool that has a length of 22m and is 9m away from the wall. Find the minimum and maximum initial velocities for the ball to land in the pool. The ramp is angled at 1 radian such that vy = 0.54v and vx = 0.84v at time zero. The ball will not be airborned for more than 3 seconds.



Homework Equations



px= vx*t

py = 12m+vy*t

vy = vy0 – t *9.81 m/s^2

vi = [vx0 vy0] (matlab)

The Attempt at a Solution



So I tried to write a loop for it. My nested for loop goes on forever...can someone please help?


s= 'DEAD';

k = 'ALIVE';

v0=0:0.01:100;
t=0:0.01:3;

for r=1:length(v0);
v00=v0(r);
vxx=v0(r)*0.84;
vyy=v0(r)*0.54;

pxi=vxx.*t;

for j = 1:length(pxi);

if pxi(j)<9;
fprintf('%s', s);

elseif pxi(j)>31;
fprintf('%s', s);

else
fprintf('%s', k);

end;

j=j+1;

end;

end;

PLEASE help! I think my approach may be wrong. If anyone can provide some pseducode :/ I suck at matlab!
 
Physics news on Phys.org
  • #2
So I realized that my inner loop will execute over over 3 million times...is there another way of approaching this? :/
 

Related to Please help my for loop goes on forever (Matlab) :/

1. Why is my for loop in Matlab going on forever?

This could be due to a few reasons. One possibility is that there is an error in the code causing the loop to never reach its ending condition. Another possibility is that the loop is stuck in an infinite loop, where the ending condition is never met. It could also be due to the loop being nested within another loop, causing it to run continuously.

2. How can I stop my for loop from running forever?

To stop a for loop from running forever, you can use a break statement within the loop. This will allow the loop to exit once a certain condition is met. You can also try debugging your code to find any errors or infinite loops that may be causing the issue.

3. What is an infinite loop?

An infinite loop is a loop that continues to run without ever reaching its ending condition. This can happen if the ending condition is never met or if there is an error in the code preventing the loop from exiting.

4. How can I prevent my for loop from getting stuck in an infinite loop?

To prevent your for loop from getting stuck in an infinite loop, it is important to carefully check your code for any errors that may cause the loop to never reach its ending condition. You can also add a counter variable to keep track of the number of iterations and set a maximum limit for the loop to run.

5. Can I use a while loop instead of a for loop to avoid infinite loops?

Yes, while loops can be used as an alternative to for loops to avoid infinite loops. While loops are controlled by a condition and will continue to run until the condition is no longer met. However, it is still important to check for any errors in your code that may cause the while loop to never exit.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Introductory Physics Homework Help
Replies
11
Views
1K
  • Introductory Physics Homework Help
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
Back
Top