Python Numerical integration in Python (throwing a ball)

Click For Summary
The discussion revolves around a physics programming problem involving the simulation of a ball thrown at an angle, accounting for gravity, air resistance, and Coriolis force. The user has successfully created arrays for position and velocity in the x and z directions but is struggling to compute the y direction. The integration of the movement equation using Python's ODEINT is yielding incorrect values, particularly resulting in a straight line when graphing velocity_y over time. Key issues identified include the use of external variables within the integration function, which complicates debugging and may lead to errors. Suggestions emphasize the importance of passing all necessary variables as parameters to the function to improve clarity and maintainability. Additionally, breaking complex assignment statements into simpler parts and printing variable values for debugging are recommended. The conversation highlights the need for better programming practices to resolve the integration issues effectively.
srecko97
Messages
82
Reaction score
13

Homework Statement


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.

Homework Equations


INTEGRATION ODEINT in PYTHON

The Attempt at a Solution


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:
Technology news on Phys.org
What is the problem? Do you get wrong values or does Python give an error?
 
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.
 
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:
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])
 
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?
 
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.
 
srecko97 said:
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.
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 21 ·
Replies
21
Views
5K
  • · Replies 41 ·
2
Replies
41
Views
6K
  • · Replies 2 ·
Replies
2
Views
17K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
10K
  • · Replies 10 ·
Replies
10
Views
2K