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

  • Thread starter Thread starter irNewton
  • Start date Start date
  • Tags Tags
    Loop Matlab
Click For Summary
SUMMARY

The forum discussion addresses a MATLAB coding issue where a nested for loop runs indefinitely due to improper loop control. The user is attempting to calculate the trajectory of a ball launched from a height of 12m, aiming to land in a pool 9m away from a wall. The initial velocities are derived from the equations of motion, specifically using the relationships between horizontal and vertical velocities. The user seeks assistance in optimizing their code to prevent excessive iterations and improve efficiency.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Knowledge of basic physics equations of motion
  • Familiarity with nested loops and control structures in programming
  • Concept of projectile motion and its parameters
NEXT STEPS
  • Optimize MATLAB loops using vectorization techniques
  • Learn about MATLAB's built-in functions for trajectory calculations
  • Study the use of conditional statements in MATLAB for efficient coding
  • Explore MATLAB debugging tools to identify and fix infinite loops
USEFUL FOR

Students learning MATLAB, physics enthusiasts working on projectile motion problems, and anyone looking to improve their programming efficiency in MATLAB.

irNewton
Messages
17
Reaction score
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
So I realized that my inner loop will execute over over 3 million times...is there another way of approaching this? :/
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
6K
Replies
11
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K