Equation of motion using Runge-kutta 4 and Verlet algorithm

In summary: T/dt) coord=[[0],[x],[y],[vx],[vy]] t=0 for i in range(1,n): x,y,vx,vy=verlet(x,y,vx,vy,dt,ax,ay) t+=dt coord[0].append(t) coord[1].append(x)
  • #1
FahdEl
3
0
Homework Statement
Hey, i used Runge-kutta 4 algorithm to solve the equation of motion (earth/sun) but the graph i get it s wrong,i thought my algorithm was wrong so i used Verlet algorithm but i had the same probleme
Relevant Equations
Equation of motion
243415

Python:
import numpy as np
import matplotlib.pyplot as plt
G=6.67408e-11
M=1.989e30
m=5.972e24
X0=-147095000000
Y0=0
VX0=0
VY0=-30300
T=365*24*60
def rk(ax,ay,x,y,vx,vy,h):
    t=0
    n=int(T/h)
    A=[[t],[x],[y],[vx],[vy]]
    for i in range(1,n):
        k1x=vx
        k1y=vy
        q1x=ax(x,y)
        q1y=ay(x,y)
        k2x=vx+h*q1x/2
        k2y=vy+h*q1y/2
        q2x=ax(x+h*k1x/2,y+h*k1y/2)
        q2y=ay(x+h*k1x/2,y+h*k1y/2)
        k3x=vx+h*q2x/2
        k3y=vy+h*q2y/2       
        q3x=ax(x+h*k2x/2,y+h*k2y/2)
        q3y=ay(x+h*k2x/2,y+h*k2y/2)
        k4x=vx+h*q3x
        k4y=vy+h*q3y
        q4x=ax(x+h*k4x,y+h*k4y)
        q4y=ay(x+h*k4x,y+h*k4y)
        x+=h*(k1x+2*k2x+2*k3x+k4x)/6
        A[1].append(x)
        y+=h*(k1y+2*k2y+2*k3y+k4y)/6
        A[2].append(y)
        vx+=h*(q1x+2*q2x+2*q3x+q4x)/6
        A[3].append(vx)
        vy+=h*(q1y+2*q2y+2*q3y+q4y)/6
        A[4].append(vy)
        t+=h
        A[0].append(t)
    return A
#acceleration Newton
def ax(x,y):
    r=(x**2+y**2)**0.5
    return -G*M*x/(r**3)
def ay(x,y):
    r=(x**2+y**2)**0.5
    return -G*M*y/(r**3)

Verlet algorithm:
import numpy as np
import matplotlib.pyplot as plt
G=6.67408e-11
M=1.989e30
m=5.972e24
X0=-147095000000
Y0=0
VX0=0
VY0=-30300
T=365
#acceleration Newton
def ax(x,y):
    r=(x**2+y**2)**0.5
    return -G*M*x/(r**3)
def ay(x,y):
    r=(x**2+y**2)**0.5
    return -G*M*y/(r**3)
#Verlet methode
def verlet(x,y,vx,vy,dt,Ax,Ay):
    x_new = x+ vx*dt + Ax(x,y)*(dt**2)/2
    y_new = y+ vy*dt + Ay(x,y)*(dt**2)/2
    vx_new = vx + dt*(Ax(x,y) + Ax(x_new,y_new))/2
    vy_new = vy + dt*(Ay(x,y) + Ay(x_new,y_new))/2
    return (x_new,y_new,vx_new,vy_new)
#simulation
def coor(x,y,vx,vy,dt):
    n=int(T/dt)
    coord=[[0],[x],[y],[vx],[vy]]
    t=0
    for i in range(1,n):
        x,y,vx,vy=verlet(x,y,vx,vy,dt,ax,ay)
        t+=dt
        coord[0].append(t)
        coord[1].append(x)
        coord[2].append(y)
        coord[3].append(vx)
        coord[4].append(vy)
    return coord
 
Physics news on Phys.org
  • #3
does it change the results ? i don't think so?what do u mean?
the probleme i must get an elliptique shape not this
 
  • #4
this graph is for x axes and y axes
 

1. What is the Equation of Motion?

The Equation of Motion is a mathematical representation of the relationship between an object's position, velocity, and acceleration over time. It is commonly used in physics and engineering to predict the motion of objects under the influence of forces.

2. What is the Runge-Kutta 4 algorithm?

The Runge-Kutta 4 algorithm is a numerical method for solving differential equations, such as the Equation of Motion. It uses a series of calculations to approximate the solution at different points in time, resulting in a more accurate prediction of the object's motion.

3. How does the Verlet algorithm differ from Runge-Kutta 4?

The Verlet algorithm is also a numerical method for solving differential equations, but it uses a different approach. Instead of using a series of calculations, it only requires the position and velocity of the object at the current time step and the previous time step. This makes it more efficient for simulating systems with conservative forces.

4. When should I use Runge-Kutta 4 versus the Verlet algorithm?

Runge-Kutta 4 is better suited for simulating systems with non-conservative forces, such as friction or air resistance. The Verlet algorithm is more efficient for simulating systems with conservative forces, such as gravitational forces.

5. Are there any limitations to using Runge-Kutta 4 and the Verlet algorithm?

Like any numerical method, Runge-Kutta 4 and the Verlet algorithm have limitations. They are both based on approximations and can introduce errors, especially when the time step is too large. It is important to carefully choose the time step and constantly check for accuracy when using these algorithms.

Similar threads

Replies
12
Views
177
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Special and General Relativity
Replies
11
Views
1K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
10
Views
12K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top