Contenido del Curso
Matlab From Beginner to Professional
Matlab From Beginner to Professional
Application: Calculating 3D Trajectories While Loops
Here you'll learn how to calculate the trajectories of objects moving in 3D space, and also another aspect of programming, while loops: whose unique properties make them just as bread and butter as for loops and if statements, although they don't come up as much.
While Loop Structure
While loops are very much like for loops in that they repeatedly execute the code placed in the middle of the statement. However, the number of times they iterate isn’t determined by a set of indices, but rather by evaluating whether the Boolean statement at the beginning of the loop is (or still is) true.
- The Boolean up top is first evaluated to see if the code in the middle gets executed at all.
- If true, the code in the middle is executed, and we return up top again and reevaluate if the Boolean is still true. If it’s still true, the code in the middle is executed again, and this repeats until the Boolean is false, whereby we immediately exit the while loop.
As shown in the video, this is extremely handy when you don’t know how many iterations you need to run!
Because while loops will repeat until the Boolean is false, there’s a risk (by circumstance or error) that the Boolean will always be true and the while loop will repeat forever! So if you find your program taking way longer than usual, it’s a good idea to stop it in matlab by tapping:
ctrl
+c
;cmd
+c
.
To stop code in progress, and reevaluate the logic and the code.
The Equation for 1D Motion With Constant Acceleration
In the video, we’re using the 1D equation of motion
- t: time (in seconds);
- x(t): position of the object at time t;
- xi: initial position of the object;
- vi: initial velocity of the object;
- a: acceleration of the object (assumed to be constant).
This is applied independently to the orthogonal x, y, z components of position, velocity, and acceleration using component-wise matrix operations.
This equation is standard in physics and is usually derived algebraically (a little more tedious, but not bad) or by integrating a constant acceleration twice over time (and recognizing that the resulting constants represent initial velocity and position).
That x, y, z are orthogonal (perpendicular) allows this to be applied to each of these dimensions separately, and at the same time we can perceive this as analyzing a single-dimension to simplify understanding.
Task
Write your own version of the program in the video by understanding the goals:
- Import the initial positions and velocities as separate matrices, and the object labels as a cell array.
- Accordion these separately into two 3D matrices so that the positions (x, y, z coordinates) of each object can evolve along the columns of its own 2D layer. We can do the same for the initial velocities (even though they don’t change- they are always the initial velocities) to make indexing consistent later on.
- For loop over the objects so that we can calculate each object’s 3D trajectory.
- Calculate each object's trajectory within a while loop whose Boolean first evaluates whether the object is still above ground (it's z coordinate is ≥ 0). Within the while loop, apply the 1D equation of motion to calculate the new position (apply it to the z, x, z coordinates independently using matrix operations). Calculating the position at ANY time should ONLY depend on:
- The object’s initial position;
- The object’s initial velocity;
- The time being evaluated.
and NOT on, say, the previous position calculated.
[OPTIONAL] Use a double for-loop to change any z-coordinates that equal zero to nan. This will prevent zeros automatically filled in by Matlab when the matrix expands from being graphed. BUT leave the initial positions untouched (by starting at the second column of the trajectory) to allow objects to start on the ground (z = 0).
Running plot3 (< x coordinates >, < y coordinates >, < z coordinates >) on any one object’s trajectory should produce a parabola [note the x, y, z components (different rows) need to be separated and input as separate variables]. Alternatively, you can finish the graphing module (or part of it!) in the next chapter as another way to confirm your results visually.
- Verifying the input data (initial positions + velocities) and the variables that accordioned these into 3D matrices is always a good way to eliminate these portions of the program as sources of error.
- Restrict the for loop to one iteration, or equivalently remove objects 2 and 3 (data and labels) from the Excel file to see how your program performs with one object.
- Non-parabolic trajectories could be symptomatic of a problem with how the equation of motion was applied, or incorrect programming of component-wise calculations (remembering periods before multiplication, etc.).
- The initial positions (first column of each 2D layer) of your 3D trajectory matrix should be the same as recorded in the Excel file.
- There shouldn’t be any negative values (each object started with a positive position and velocity in all x, y, z directions).
- Check how acceleration was defined, or try setting it equal to zero. This should produce motion at constant velocity, which should form lines when plotted (if you take the top view of the 3D graph, which only shows motion in the x, y directions where acceleration was zero, you’ll see the same thing).
- According to our coordinate system, gravity is -9.8 m/s² (downwards towards the Earth), not +9.8.
¡Gracias por tus comentarios!