Numerical integration in Python (throwing a ball)

Click For Summary

Discussion Overview

The discussion revolves around a programming challenge related to numerical integration in Python for simulating the motion of a ball thrown at an angle, considering forces such as gravity, air resistance, and Coriolis force. The focus is on integrating the equations of motion to derive the position and velocity in the y-direction, while the participants explore various coding approaches and troubleshoot issues with the output.

Discussion Character

  • Homework-related, Technical explanation, Debate/contested

Main Points Raised

  • The original poster describes their approach to modeling the motion of a ball and outlines the equations used for integration, specifically mentioning the need to account for Coriolis force.
  • Some participants inquire about the nature of the problem, specifically whether the issue lies in incorrect values or errors generated by Python.
  • The original poster clarifies that they are receiving incorrect values, noting that the graph of velocity_y over time appears as a straight line, which they believe is incorrect.
  • Another participant suggests an alternative approach to calculating velocity and position, indicating that while the results seem acceptable, the mathematical method used may not be optimal.
  • One participant critiques the original integration function, pointing out that it relies on external variables not passed as parameters, which complicates debugging and understanding the function's behavior.
  • There is a repeated emphasis on the importance of good programming practices, particularly regarding the use of global variables and the complexity of assignment statements within functions.
  • Participants express the need for more context or additional code to effectively assist with debugging the issues presented.

Areas of Agreement / Disagreement

Participants generally agree on the importance of good programming practices and the need for clarity in the code. However, there is no consensus on the specific issues causing the incorrect output, and multiple approaches to solving the problem are presented without resolution.

Contextual Notes

The discussion highlights potential limitations in the original code, including reliance on external variables and complex expressions that may hinder understanding and debugging. The exact definitions and values of the variables used in the integration function are not fully detailed, which may affect the ability to diagnose the problem.

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 50 ·
2
Replies
50
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 2 ·
Replies
2
Views
17K
  • · Replies 41 ·
2
Replies
41
Views
6K