Skip to main content
Physics LibreTexts

4.6: Using Computation to Simulate Motion in Multiple Dimensions

  • Page ID
    91212
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Let's continue where we left off with computationally simulating one-dimensional motion. We saw that we can write vectors in Visual Python using the syntax.

    r = vec(x, y, z)

    where x, y, and z are the components of the position vector r. Of course, we can write a vector of any quantity. For example, position, velocity, or acceleration. Continuing the example of a ball moving from before, try adding a velocity component of \(v_y = 0.5\) m/s by editing ball.vel. This will create motion in the y-direction that is the same as the x-direction. You may start from the code that ended the previous computational simulation. We will make a small change first. In the definition of the ball, we are going to create a trail of dots that shows the ball's past positions, and we will remove the velocity arrow for the this example.

    ball = sphere(pos=vec(0,0,0), color=color.green, radius=0.1, make_trail=True, trail_type="points")
    ball.vel = vec(0.5, 0, 0)
    r_0 = ball.pos
    t = 0
    dt = 1
    while t<10:
        rate(1)
        ball.pos = r_0 + ball.vel * t
        t = t + dt 
    

     

    Next, let's look at an example from an earlier section that includes acceleration. The example is shown again below.

    Exercise \(\PageIndex{1}\)

    An object starts at the origin of a coordinate system at time \(t=0s\), with an initial velocity vector \(v_0=(10m/s)\hat{x}+(15m/s)\hat{y}. The acceleration in the \(x\) direction is \(0m/s^2\) and the acceleration in the \(y\) direction is \(−10 m/s^2\).

    1. Write an equation for the position vector as a function of time.
    2. Determine the position of the object at \(t=10s\).
    3. Plot the trajectory of the object for the first \(5s\) of motion.
    Answer
    scene2 = canvas(title='Examples of 2D Accelerated Motion',
         width=400, height=400,
         center=vector(5,0,0), background=color.black,
         range=300)
    
    ball = sphere(pos=vec(0,0,0), color=color.green, radius=10, make_trail=True, trail_type="points")
    ball.vel = vec(10, 15, 0)
    ball.acc = vec(0, -10, 0)
    r_0 = ball.pos
    t = 0
    dt = 1
    while t<=10:
      rate(1)
      ball.pos = r_0 + ball.vel * t + 0.5 * ball.acc * t**2
      t = t + dt
    
    print(ball.pos)

    To solve this we need to do several things to our constant velocity code above. First, we need to edit the initial velocity, ball.vel, using what is given in the example. Then, we should define ball.acc = vec(0, -10, 0). Third, we should make the while loop go until less than or equal to 10s by writing while t <= 10:. Fourth, inside the loop, we need to edit the ball.pos to include the acceleration. In Python, raising to a power is done by two asterisks, e.g., t**2 is the square of t. The time-dependent position is

    \[\vec{r}(t) = \vec{r}_0 + \vec{v}_0 t + \frac{1}{2} \vec{a}t^2\]

    will be written in Visual Python as

    ball.pos = r_0 + ball.vel * t + 0.5 * ball.acc * t**2
    

    Finally, we can print the ball's position after the loop finishes with the command print(ball.pos). This print statement should be unindented after the while loop to let Python know that it occurs after the looping is complete. Of course, you are welcome to put the print statement in the loop (indented under t = t + dt) if you want to see what difference this makes. Making these changes we can show the position vs. time of the object and print the position after 10 s have passed. To summarize, the steps are

    1. Edit ball.vel.
    2. Create an acceleration of the ball, ball.acc.
    3. Make the loop end at 10 s.
    4. Edit ball.pos in the loop to include ball.acc.
    5. Add a print statement to see the ball's position at \(t=10 s\).

    Below is a trinket that will keep the scene fixed. Copy the constant velocity code above and make these five edits. The ball radius may need to be increased if it is difficult to see. If you get stuck look at the answer to the example.

     

    It's not necessary to simulate the motion if we only want to calculate or graph. We can use Visual Python to calculate the position of an object as we did above. To do this, we simply define the initial position, initial velocity, and acceleration. Then, we can use the kinematic equation to calculate the position at a later time. Try putting this into the trinket above.

    r0 = vec(0,0,0)
    v0 = vec(10, 15, 0)
    a = vec(0, -10, 0)
    
    t = 10
    
    rt = r0 + v0*t + 0.5*a*t**2
    print(rt)
    

    Similarly, we can use Python (not Visual Python) to plot. You'll notice the trinket below says Python3. This distinguishes it from the other trinkets above. It also does not have the first line that imports Visual Python (Web VPython 3.2).

    To make lists (arrays) of numbers, it is best to use a library called numpy. To make graphs also requires importing a library matplotlib for making graphs. Using the command import a as b makes library a available by using b to call it. 

    import numpy as np
    import matplotlib.pyplot as plt

    We won't use vectors, but we keep our initial values for \(x_0, y_0, v_{x0}, v_{y0}, a_x\), and \(a_y\). Then, we need to make lists of \(x(t), y(t)\), and \(t\) that correspond to the points we want to graph. These are calculated using \(x_0, y_0, v_{x0}, v_{y0}, a_x, a_y\), and the list of times \(t\). Therefore, we must define \(t\) before attempting to calculate \(x(t)\), and \(y(t)\)

    x_0 = 0
    y_0 = 0
    v_x0= 10
    v_y0 = 15
    a_x = 0
    a_y = -10
    t = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
    
    xt = x_0 + v_x0 * t + 0.5 * a_x * t**2
    yt = y_0 + v_y0 * t + 0.5 * a_y * t**2
    

     That will create lists of \(x(t)\) and \(y(t)\) because Python recognizes that \(t\) is a list and calculates for each value.

    Finally, we insert the code to make a graph.

    plt.plot(xt, yt, 'or')
    plt.xlabel('x (m)')
    plt.ylabel('y (m)')
    plt.show()
    

    The first line plots  xt on the \(x\) axis and yt on the \(y\) axis. The 'or' makes circles for the points that are red. The second line labels the \(x\) axis. The third line labels the \(y\) axis. The last line puts all the plot code into an image. Try combining these three blocks of code in the trinket below to see that they plot the 2D motion of the object.


    4.6: Using Computation to Simulate Motion in Multiple Dimensions is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?