Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Application: Calculating 3D Trajectories While Loops | Visualizations
Matlab Basics
course content

Conteúdo do Curso

Matlab Basics

Matlab Basics

1. Basic Syntax and Coding with a Text Editor
2. Coding Foundations
3. Learning Through Applications
4. Visualizations
5. Recursion and Matrix Multiplication

book
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 similar to for loops, but their key difference lies in the control mechanism for iteration. Instead of a fixed number of iterations, the while loop runs as long as the Boolean condition is true. Here’s how it works:

  • The Boolean condition is evaluated first;
  • If the condition is true, the code inside the loop is executed;
  • After the code runs, the condition is checked again. If it’s still true, the code is executed again. This continues as long as the condition remains true;
  • Once the condition becomes false, the loop exits, and the program continues from the point after the loop.

This structure is particularly useful when you don’t know in advance how many times the loop will need to run. It keeps executing until a certain condition is met, making it great for tasks where the number of iterations depends on dynamic factors.

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:

  1. Import the initial positions and velocities as separate matrices, and the object labels as a cell array;

  2. 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;

  3. For loop over the objects so that we can calculate each object's 3D trajectory;

  4. Calculate each object's trajectory within a while loop whose Boolean first evaluates whether the object is still above ground (its z coordinate is ≥ 0). Within the while loop, apply the 1D equation of motion to calculate the new position (apply it to the x, y, 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.

It should not depend on the previous position calculated.

  • 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.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt