#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Roanoke, TX
    Posts
    3
    Rep Power
    0

    Help a physics student? :)


    Hello all, I am trying to write a simple script to visualize the trajectory of a soccer ball using python (for an extra credit assignment in my mechanics class). I am very familiar with Java, C++ and VB, but not so much with python, especially visual stuff. I'm hoping you all can help!

    I've got the following script which plots the trajectory of the ball beautifully. However, the ball hits the "earth" (z axis = 0), and keeps going. My thoughts are that I could change this behavior using a while loop. Like, while z >= 0, plot the trajectory.

    Then, change the vertical velocity from its current negative value to a positive value (making the ball "bounce").

    Then run another while loop... I could have 3 while loops for 3 bounces. It doesn't need to be an efficient program (this isn't a CS class), it just needs to work.

    Here's my script:


    import matplotlib as mpl
    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    import matplotlib.pyplot as plt

    mpl.rcParams['legend.fontsize'] = 10

    fig = plt.figure()
    ax = fig.gca(projection='3d')


    ### Define Variables ###
    time = (10) #Time to calculate the equations
    t = np.linspace(0, time, 100)

    g = (-9.81) #Gravity

    vxo = (3) #initial velocity in the x-direction
    vyo = (1) #initial velocity in the y-direction
    vzo = (15) #initial velocity in the z-direction

    xo = (0) #initial x-position
    yo = (0) #initial y-position
    zo = (0) #initial z-position


    ### Equations of Motion ###
    x = (xo + (vxo * t))
    y = (yo + (vyo * t))
    z = (zo + (vzo * t) + (.5 * g * (t**2)))


    #Here is where I am thinking I need to insert a while loop??
    #While (z >= 0):
    ax.plot(x, y, z, label=('Trajectory of Soccer Ball ' + str(time)))
    ax.legend()

    #Then flip the sign on the vertical velocity:
    #vzo = (-1 * vzo)

    #then continue to plot now that the ball is moving back UP

    ### Set axis limits ###
    ax.set_xlim3d(0,10)
    ax.set_ylim3d(0,10)
    ax.set_zlim3d(0,10)
    plt.show()
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    157
    Rep Power
    2
    I don't know anything about matplotlib or numpy but maybe I can help with the loop. Let's say you have (as you do appear to) the correct eq of motion for some time period: t=0-100, for instance, with an interval of 1 (just to make it convenient). This could be seconds, fortnights, or any units of time as long as they are the independent variable in the equations. Now, somewhere in your code, either matplotlib or numpy or something is doing the time loop for you but I don't know where so let's put it out there:

    Code:
    for t in xrange(100): #that samples t from 0 to 100 at dt=1
       x,y,z=AnotherFunction(t,xdot,ydot,zdot,x,y,z) #your eq's of motion in this function
       if z<0: 
          zdot=-zdot
       else: <plot>
    This should bounce as many times as it needs to within the timeframe
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Roanoke, TX
    Posts
    3
    Rep Power
    0
    I like that, it helps simplify things. Now I need to figure out how to make it work with numpy and matplotlib...

    It would seem that my variables x, y, and z are actually treated like arrays (time in one column, and position in the other)

    For instance,
    z:
    0,0
    1,1
    2,4
    3,7
    4,12


    So you can see time increasing steadily in one column, and the position is being calculated by the equation of motion. THEN the program plots these values.

    If I can somehow deconstruct how this is happening, I can more intelligently manipulate it... Any ideas there?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    time is a vector, all the solutions are vectors. You don't need a "while loop"

    You could install the statement

    z = abs(z)

    right before your comment
    #Here is where I am thinking I need to insert a while loop??


    Well, that's completely *!&%ed up because the velocity should change sign non-linearly, and the new parabola should be concave down.

    I'd integrate the equations of motion. http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html with a velocity function that checks the height. Using the ode you could install realistic drag and bounce friction.

    Much faster if you don't care about the drag and solve the equations piece-wise. You might consider learning how to index arrays or the numpy.concatenate function.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Roanoke, TX
    Posts
    3
    Rep Power
    0
    I got it; it just took some clever thinking.

    Here's what I got, using nested for loops:

    Code:
    for i in range(0,fineness):
        if z[i] < 0:
            zvo = (-zv[i])
            
            for j in range(i,fineness):
                z[j] = ((zvo * t[(j-i)]) + (.5 * g * (t[(j-i)]**2)))
                v[j] = (zvo + (g*t[(j-i)]))
    fineness is a variable I use to control how many samples are taken in the time range specified. So, from t 0 to 10, I could have 10 samples (very inaccurate) or 1000 samples (much more accurate).

    Basically, I first calculate the value of Z from t = 0 to 10. Then, I have this for loop test to see if one particular value of Z has gone below 0. If it has, the CURRENT velocity in the Z direction is set to a positive value, and used as the INITIAL velocity in the Z direction going forward.

    The nested For loop rewrites every value of the position array Z from the moment it went sub-0 to the end of the time range. It will re-write it as many times as necessary to cover the time range (so possible a few times).

    I know this method isn't perfect or pretty, and it would get some marks down for unnecessary repetitiveness from a CS prof, but this is going to my physics prof who could care less, as long as it works. With my machine, it takes a fraction of a second to calculate, so that's fine with me.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    I'm glad it's solved. If you index items one at a time numpy provides little benefit.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo