Two Body problem using Verlet Algorithm Python

In summary, the conversation discusses a code written in Python using the Verlet algorithm to solve a two body problem. The code includes functions for position, force, and potential, and a for loop to obtain positions in every dt step. The conversation also addresses an error involving multiplying a float and a function and questions about the correctness of the for loop and position definition.
  • #1
Zaknife
12
0

Homework Statement



Hi !

I'm trying to solve numerically two body problem using Verlet algorithm in Python. I wrote a code which looks like that :

Code:
import numpy as np
import scipy as sp
rm=np.array([0.,0.])
r0=np.array([2.,0.])
p0=np.array([0.,0.1])
dt=0.001
m=0.1
G=0.01
M=500.0def r(dt):
    if i == 0:
        return r0
    else: 
        return r == np.array([0.,0.])
def force(r): 
    return -G * M * m * r / np.sqrt(np.mdot(r,r,r))
def potential(r):
    return -G * M * m * r / np.sqrt(np.vdot(r,r))
for i in range(1,50) :
    r(i*dt+dt)=2*r(dt)-r(i*dt-dt)(force(r)/m)*i*dt**2
    print r

My questions are:

1)When I'm trying to compile this code follolwing error occurs:
TypeError: unsupported operand type(s) for *: 'float' and 'function'
2)I'm not sure , if this for loop works correctly, because in the end i'd like to obtain a positions in every dt step. If positions r(dt) is defined correctly ?


Thanks in advance !
 
Last edited:
Technology news on Phys.org
  • #2
Homework Equations r(i*dt+dt)=2*r(dt)-r(i*dt-dt)(force(r)/m)*i*dt**2The Attempt at a Solution1) The error occurs because you are attempting to multiply a float and a function, which is not allowed.2) The for loop looks correct. The position r(dt) should be defined correctly if the other variables in the equation have been defined correctly.
 

1. What is the Two Body problem?

The Two Body problem is a mathematical problem in physics that involves determining the motion of two point masses that are gravitationally interacting with each other. It is an important problem in celestial mechanics and has applications in understanding the motion of planets, moons, and other celestial bodies.

2. What is the Verlet Algorithm?

The Verlet Algorithm is a numerical integration algorithm used to solve the equations of motion for a system of particles. It is commonly used in physics simulations, including simulations of the Two Body problem. The algorithm is based on a simple iterative process and is known for its stability and accuracy.

3. How is the Two Body problem solved using the Verlet Algorithm in Python?

In Python, the Two Body problem can be solved using the Verlet Algorithm by first defining the initial conditions of the two bodies, including their masses, positions, and velocities. Then, the algorithm is implemented using a loop to calculate the positions and velocities of the bodies at each time step. The positions can then be plotted to visualize the motion of the bodies over time.

4. What are the advantages of using the Verlet Algorithm to solve the Two Body problem?

The Verlet Algorithm has several advantages when solving the Two Body problem. It is a simple and efficient algorithm, making it easy to implement in code. It is also known for its stability and accuracy, which is important when simulating the motion of celestial bodies. Additionally, the algorithm is symplectic, meaning it conserves energy and momentum, making it a more realistic simulation of the Two Body problem in physics.

5. Are there any limitations to using the Verlet Algorithm to solve the Two Body problem?

While the Verlet Algorithm is a powerful tool for solving the Two Body problem, it does have some limitations. One limitation is that it assumes the bodies are point masses and do not have any physical size. This may not accurately reflect the real world, where objects typically have finite sizes. Additionally, the algorithm may struggle with highly eccentric orbits or close encounters between the bodies, requiring smaller time steps to maintain accuracy.

Similar threads

  • Programming and Computer Science
Replies
2
Views
856
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
868
  • Programming and Computer Science
Replies
8
Views
2K
Replies
9
Views
942
  • Programming and Computer Science
Replies
18
Views
998
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top