1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Numerical integration in Python (throwing a ball)

  1. May 23, 2017 #1
    1. The problem statement, all variables and given/known data
    I have a problem with my physics task, but you do not need to understand physics to be able to help me, because my main problem is bad programming skill. I am dealing with a problem of throwing a ball in the air at an angle between 0 an 45 degrees. I need to consider not only the gravity, but also the force of air resistance and Coriolis force. I am treating the movement in x (distance) and in z(height) directions separately using Newton's laws without any problems. So I got 4 arrays with 20000 elements of position in both directions and velocity in both directions: velocity_x, velocity_z, position_x, position_z and an array with angles of velocity, called angles. These arrays are 100% correct. Also all the parameters S, mass, rho, omega, zero are OK. Now I need to deal with the y direction (perpendicular to x and z). Coriolis force moves the ball in the direction of y. Size of Coriolis force depends on the angle between the vector of velocity and angular velocity of earth and the size of it. The main problem is integrating my movement equation using previously formed arrays. I want to get two new arrays: position_y and velocity_y.

    2. Relevant equations
    INTEGRATION ODEINT in PYTHON

    3. The attempt at a solution
    Code (Python):
    #   Ty = [y,v_y]
    #   Ty' = [y',v_y'] y'=v_y
    #v_y' = a = -2 * omega *velocity* m.sin(-angle+0.785398163)-v_y/abs(v_y)*0.5*rho*S*v_y**2/mass
    #velocity=(velocity_x[n]**2 + velocity_z[n]**2+Ty[1]**2)**0.5
    #v_y'=-2 * omega *(velocity_x[n]**2 + velocity_z[n]**2+Ty[1]**2)**0.5 * m.sin(-angles[n]+0.785398163)-Ty[1]/abs(Ty[1])*0.5*rho*S*Ty[1]**2/mass

    def dTy_dt (Ty,t):
       return[Ty[1],-2 * omega *(velocity_x[n]**2 + velocity_z[n]**2+Ty[1]**2)**0.5 * m.sin(-angles[n]+0.785398163)-Ty[1]/abs(Ty[1])*0.5*rho*S*Ty[1]**2/mass]

    Ty0=[0,0]
    time=np.linspace(0, zero, 20000)
    Tys=integrate.odeint(dTy_dt, Ty0, time)
    position_y = Tys[:,0]
    velocity_y = Tys[:,1]
     
    Last edited by a moderator: May 23, 2017
  2. jcsd
  3. May 23, 2017 #2
    What is the problem? Do you get wrong values or does Python give an error?
     
  4. May 23, 2017 #3
    Sorry. I forgot to write that. I get the wrong values. And if i draw a graph veloctiy_y (time) I got a straight line. This is wrong.
     
  5. May 23, 2017 #4
    i tried also like that: I think the results are quite OK, but the way i got them is mathematically not the best i think
    Code (Text):
    coriolisiandresistance = [None] * 20000
       velocity_y = [None] * 20000
       position_y=[None] * 20000
       velocity_y[0]=0.0000000000000000000000000001
       position_y[0]=0
       tsy=np.linspace(0, nicla, 20000)
      for n in range(1,20000)
               coriolisandresistance[n]= - 2 * omega *(velocity_x[n]**2 + velocity_z[n]**2+velocity_y[n-1]**2)**0.5 * m.sin(-angles[n]+0.785398163-velocity_y[n-1]/abs(velocity_y[n-1])*0.5*rho*S*velocity_y[n-1]**2/mass)

           velocity_y[n]=velocity_y[n-1]+coriolisandresistance[n]*(tsy[n]-tsy[n-1]) #v[n]=v[n-1]+a[n]*(t[n]-t[n-1])
           position_y[n]=position_y[n-1]+velocity_y[n]*(tsy[n]-tsy[n-1]) #x[n]=x[n-1]+v[n]*(t[n]-t[n-1])
     
     
  6. May 23, 2017 #5

    Mark44

    Staff: Mentor

    I don't see how your program can work. In the function that you integrate, dTy_dt, there are two parameters -- Ty and t -- but the function uses lots of variables whose values can't be determined, at least from the code you posted.
    • omega
    • velocity_x (an array or list)
    • n
    • velocity_z (an array or list)
    • m
    • angles (an array or list)
    • rho
    • S
    • mass
    It's bad programming practice for a function to work with or modify variables that aren't in its parameter list; i.e., variables that are defined outside the function. Using variables in this way makes debugging the code extremely difficult, something you're probably running into.

    Is there more to your code than you have posted?
     
  7. May 23, 2017 #6
    All the listed variables and lists are defined as it is written in descrtiption of my problem. I have posted just the last part of the code.
     
  8. May 23, 2017 #7

    Mark44

    Staff: Mentor

    We can't help you with your code if we can't see it. There's no possible way for us to tell what your function is doing without knowing the values of the variables it is using.

    As mentioned already, it's very bad programming practice to write a function that uses variables other than those passed as arguments. Further, your function has a single-line assignment statement that is extremely complex. It would be better to print the values of all variables used in that function, and break that long assignment statement up into about three or four assignments statements.

    What is the code you have in post #4? Is that output? I don't see how it is related to what you have in post #1, which doesn't have anything to do with Coriolis forces.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Numerical integration in Python (throwing a ball)
  1. Throwing a ball (Replies: 8)

  2. Throwing ball (Replies: 7)

  3. Throwing a Ball (Replies: 15)

  4. Throwing a ball (Replies: 9)

  5. Throwing Balls (Replies: 3)

Loading...