Numerical integration in Python (throwing a ball)

In summary, the problem is that the user is trying to integrate a movement equation using arrays that were previously formed. The user is trying to get two new arrays, position_y and velocity_y, but is getting incorrect values.
  • #1
srecko97
82
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
  • #2
What is the problem? Do you get wrong values or does Python give an error?
 
  • #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.
 
  • #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:
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])
 
  • #5
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?
 
  • #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.
 
  • #7
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.
 

Related to Numerical integration in Python (throwing a ball)

1. How can I perform numerical integration in Python?

To perform numerical integration in Python, you can use the built-in function scipy.integrate.quad() or the scipy.integrate.odeint() function from the scipy library. These functions allow you to integrate a function over a specified range or solve a differential equation respectively.

2. Can I use numerical integration to model the trajectory of a thrown ball in Python?

Yes, you can use numerical integration to model the trajectory of a thrown ball in Python. By using the equations of motion and inputting the necessary initial conditions, you can use the scipy.integrate.odeint() function to solve for the position and velocity of the ball at different time intervals. This will give you a numerical representation of the ball's trajectory.

3. How do I plot the trajectory of a thrown ball using numerical integration in Python?

To plot the trajectory of a thrown ball using numerical integration in Python, you can use the matplotlib library. After obtaining the position and velocity values from the scipy.integrate.odeint() function, you can use the plt.plot() function to create a line graph of the ball's trajectory.

4. Can I incorporate air resistance into my numerical integration model of a thrown ball in Python?

Yes, you can incorporate air resistance into your numerical integration model of a thrown ball in Python. You can do this by including the effects of air resistance in your equations of motion and using the scipy.integrate.odeint() function to solve the resulting differential equations. This will give you a more realistic simulation of the ball's trajectory.

5. Are there any limitations to using numerical integration for modeling a thrown ball in Python?

There are some limitations to using numerical integration for modeling a thrown ball in Python. The accuracy of the model depends on the time step used and the complexity of the equations of motion. Additionally, factors such as air resistance and external forces may not be accurately represented in the model. Therefore, it is important to carefully consider the limitations and assumptions of the model when using numerical integration in this context.

Similar threads

  • Programming and Computer Science
Replies
21
Views
4K
  • Programming and Computer Science
Replies
2
Views
16K
  • Programming and Computer Science
2
Replies
41
Views
4K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Replies
5
Views
399
  • Advanced Physics Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
10K
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
4
Views
11K
Back
Top