Help in Python for Computational Physics

In summary, the backspin on a fastball has a significant affect on its trajectory. The Euler Method is used to solve for the trajectory.
  • #1
source.decay
3
0
Hi, I am in a computational physics class and we are programming projectile motion in python. I do not know why I am having such a hard time at this, but I cannot get my programs to work.

Homework Statement


Calculate the effect of backspin on a fastball. How much does an angular velocity of 1000 rpm affect the trajectory. We are incorporating both drag and spin into the equations. I am the Euler method to solve the problem.

Homework Equations


Drag: F(v) = 0.0039 + 0.0058/(1 + exp((v-35)/5)) (As an aside, there is a graph in my book modeling this equation and I cannot replicate it. It shows that at about 25 mph the function starts at .5 and at 55 or so, there is an exponential drop to .2)

dx/dt = vx
dy/dt = vy ,
dz/dt = vz ,

dvx/dt = −F(v) v vx + B ω (vz sin φ − vy cos φ),

dvy/dt = −F(v) v vy + B ω vx cos φ,

dvz/dt = −g − F(v) v vz − B ω vx sin φ.

v = 44.7 m/s
B = 4.1*10^-4 (unitless)
φ = 225 degrees
ω = 100pi/3 rad/s x(t = 0) = 0,
y(t = 0) = 0,
z(t = 0) = 3,
vx (t = 0) = v0 cos θ
vy (t = 0) = 0,
vz (t = 0) = v0 sin θ,

The Attempt at a Solution



Below is the code that I have so far.

from scipy import *
from pylab import *
from numpy import *

#Define Constants
m = .149 #mass of baseball in kilograms
B = (4.1*10**-4) #Spin coefficient - unitless
w = (100*pi)/3 #1000 rpm in rad/s
g = 9.81 #acceleration due to gravity m/s^2

theta = 1
pho = 225

#Define Arrays
dt = 1*10**-4
n = int(1/dt)
phi = pi/180 #Convert degrees to radians

t = linspace(0,0,n+1)
x = linspace(0,0,n+1)
y = linspace(0,0,n+1)
z = linspace(0,0,n+1)
vx = linspace(0,0,n+1)
vy = linspace(0,0,n+1)
vz = linspace(0,0,n+1)
v = linspace(0,0,n+1)
F = linspace(0,0,n+1)#Initial Conditions
x[0] = 0 #Axis is oriented os that x is the line from the pitcher's mound to homeplate
y[0] = 0
z[0] = 3
v[0] = 44.704 #Initial speed of the ball in m/s (100mph)
F[0] = 0.0039 + (0.0058)/(1 + exp((v[0] - 35)/5))


vx[0] = v[0]*cos(theta*phi) #the ball is initially thrown at a small 1 degree angle above the horizontal
vy[0] = 0 #No inital velocity in the y direction
vz[0] = v[0]*sin(theta*phi)
i = 0
for i in xrange(n):
t[i+1] = t + dt
v[i+1] = sqrt(vx**2 + vy**2 + vz**2)
F[i+1] = 0.0039 + (0.0058)/(1 + exp((v - 35)/5))
vx[i+1] = -F * v * vx + B * w *(vz*sin(pho*phi) - vy*cos(pho*phi))
x[i+1] = x + vx*dt
y[i+1] = y + vy*dt
vy[i+1] = -F * v * vy + B * w * vx * cos(pho*phi)
z[i+1] = z + vz*dt
vz[i+1] = -g - F * v * vz - B * w * vx * sin(pho*phi)
print(v, x, y, z, F, t)
if z < 0:
break
 
Technology news on Phys.org
  • #2
#Plottingplot(x,y)show()I realize that there are some syntax errors and logic errors in my code. I am having a hard time understanding where they are coming from. Any help would be greatly appreciated!
 
  • #3


print("Max Height: ", max(z))
print("Distance: ", max(x))

#Plot Trajectory
plot(x,y)
title("Projectile Motion")
xlabel("x (m)")
ylabel("y (m)")
show()

First of all, don't get discouraged! Programming can be challenging, especially when dealing with complex equations and simulations. Here are a few tips that might help you:

1. Make sure you understand the problem and the equations you are using. It's important to have a clear understanding of the physical concepts and how they relate to the code you are writing. If you are having trouble replicating the graph from your book, try plotting it using the same equation to see if you get the same results. This will help you identify any errors in your code or understand the behavior of the equation better.

2. Break down the problem into smaller, manageable parts. It can be overwhelming to try and tackle the entire problem at once. Instead, try breaking it down into smaller steps and solving them one at a time. For example, you can first write a code to simulate projectile motion without spin and drag, and then gradually add in those factors.

3. Use comments and descriptive variable names. This will make your code more readable and easier to troubleshoot. It's also helpful to write down your thought process in comments, so you can come back to it later and understand what you were trying to do.

4. Debug your code step by step. If you are having trouble getting your code to work, try printing out the values of different variables at each step to see if they make sense. This can help you identify where the problem is and how to fix it.

5. Seek help if needed. Don't hesitate to ask for help from your classmates, instructor, or online resources. There are many forums and communities dedicated to coding and computational physics that can offer support and guidance.

Good luck with your project! Keep practicing and don't give up, you will get there.
 

1. What is Python and why is it useful for computational physics?

Python is a high-level, interpreted programming language that is widely used in scientific computing and data analysis. It has a simple and intuitive syntax, making it easy to learn and use. Python is useful for computational physics because it offers a wide range of libraries and tools for numerical calculations, data visualization, and scientific computing. Its flexibility and versatility make it well-suited for solving complex physics problems and performing simulations.

2. How can I get started with using Python for computational physics?

To get started with using Python for computational physics, you will need to first install the Python programming language on your computer. You can then install additional libraries and tools such as NumPy, SciPy, and Matplotlib, which are commonly used in scientific computing. There are also many online resources and tutorials available to help you learn the basics of Python and its applications in physics.

3. What are some common challenges when using Python for computational physics?

Some common challenges when using Python for computational physics include dealing with large datasets, optimizing code for performance, and debugging complex programs. It is important to have a good understanding of the underlying principles of physics and the mathematical concepts involved in order to effectively use Python for solving physics problems.

4. Can I use Python for both classical and quantum physics simulations?

Yes, Python can be used for both classical and quantum physics simulations. There are many libraries and tools available that specifically cater to quantum computing, such as Qiskit and Cirq. However, Python is also commonly used for classical physics simulations, making it a versatile language for a wide range of physics applications.

5. Are there any limitations to using Python for computational physics?

While Python is a powerful and popular language for scientific computing, it does have some limitations when it comes to computational physics. One limitation is its performance compared to lower-level languages such as C or Fortran. However, with careful optimization and the use of specialized libraries, these limitations can be mitigated. Another limitation is the lack of built-in support for parallel computing, although there are libraries such as mpi4py that enable parallel processing in Python.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
589
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
841
  • Programming and Computer Science
Replies
3
Views
919
  • Programming and Computer Science
Replies
14
Views
4K
Back
Top