简体   繁体   English

在 python 中使用四阶龙格库塔求解 3 个耦合非线性微分方程

[英]Solving 3 coupled nonlinear differential equations using 4th order Runge Kutta in python

I am trying to plot orbits of a charged particle around a Reissner–Nordström blackhole(Charged blackhole).我正在尝试 plot 带电粒子围绕 Reissner–Nordström 黑洞(带电黑洞)的轨道。

I have three 2nd order differential equations as well as 3 first order differential equations.我有三个二阶微分方程和三个一阶微分方程。 Due to the nature of the problem each derivative is in terms of proper time rather than time t.由于问题的性质,每个导数都是根据适当的时间而不是时间 t。 The equations of motion are as follows.运动方程如下。

2 first order differential equation second order differential equations 2 一阶微分方程 二阶微分方程

3 second order differential equations 3个二阶微分方程

1 first order differential equation(there should be a negative multiplied by everything under the square root. 1 一阶微分方程(应该有一个负数乘以平方根下的所有值。

I am using the 4th order Runge Kutta method to integrate orbits.我正在使用四阶龙格库塔方法来整合轨道。 My confusion, and where I most likely am making a mistake comes from the fact that usually when you have a second order coupled differential equation you reduce it into 2 first order differential equations.我的困惑,我最有可能犯的错误来自这样一个事实,即通常当你有一个二阶耦合微分方程时,你会将它简化为 2 个一阶微分方程。 However in my problem I have been given 3 first order differential equations along with their corresponding second order differential equations.然而,在我的问题中,我得到了 3 个一阶微分方程以及它们相应的二阶微分方程。 I assumed since I was given these first order equations I wouldn't have to reduce the second order at all.我假设因为我得到了这些一阶方程,所以我根本不需要减少二阶。 The fact that these equations are non linear does complicate things further.这些方程是非线性的这一事实确实使事情变得更加复杂。

I am sure I can use Runge kutta to solve such problems however I'm not sure about my implementation of the equations of motion.我确信我可以使用 Runge kutta 来解决这些问题,但是我不确定我对运动方程的实现。 When I run the code I get an error that a negative number is under the square root of F2, however this should not be the case because F2 should equal exactly zero (Undoubtedly a precision issue arising from F1).当我运行代码时,我收到一个错误,即负数在 F2 的平方根之下,但事实并非如此,因为 F2 应该正好等于零(无疑是 F1 引起的精度问题)。 However even when I take the absolute value of everything under the square root of F1,F2,F3... my angular momentum L and energy E is not being conserved.然而,即使我在 F1、F2、F3 的平方根下取一切的绝对值...我的 angular 动量 L 和能量 E 也没有被守恒。 I mainly would like someone to comment on the way I am using my differential equation inside my Runge kutta loop and inform me how I am supposed to reduce the 2nd order differential equations.我主要希望有人评论我在 Runge kutta 循环中使用微分方程的方式,并告诉我应该如何减少二阶微分方程。

import matplotlib.pyplot as plt
import numpy as np
import math as math
#=============================================================================
h=1
M         = 1                  #Mass of RN blackhole
r         = 3*M                #initital radius of particle from black hole
Q         = 0                  #charge of particle
r_s       = 2*M                #Shwar radius
S         = 0                  # initial condition for RK4
V         = .5                 # Initial total velocity of particle
B         = np.pi/2            #angle of initial velocity
V_p       = V*np.cos(B)        #parallel velocity
V_t       = V*np.sin(B)        #transverse velocity
t         = 0
Theta     = 0
E         = np.sqrt(Q**2-2*r*M+r**2)/(r*np.sqrt(1-V**2))
L         = V_t*r/(np.sqrt(1-V**2))
r_dot     = V_p*np.sqrt(r**2-2*M+Q**2)/(r*np.sqrt(1-V**2))
Theta_dot = V_t/(r*np.sqrt(1-V**2))
t_dot     = E*r**2/(r**2-2*M*r+Q**2)

#=============================================================================
while(r>2*M and r<10*M):   #Runge kutta while loop
    A1 = 2*(Q**2-M*r) * r_dot*t_dot / (r**2-2*M*r+Q**2)                                           #defines T double dot fro first RK4 step
    B1 = -2*Theta_dot*r_dot / r                                                                   #defines theta double dot for first RK4 step
    C1 = (r-2*M*r+Q**2)*(Q**2-M*r)*t_dot**2 / r**5 + (M*r-Q**2)*r_dot**2 / (r**2-2*M*r+Q**2)      #defines r double dot for first RK4 step
    D1 = E*r**2/(r**2-2*M*r+Q**2)                                                                 #defines T dot for first RK4 step
    E1 = L/r**2                                                                                   #defines theta dot for first RK4 step
    F1 = math.sqrt(-(1-r_s/r+Q**2/r**2) * (1-(1-r_s/r+Q**2/r**2)*D1**2 + r**2*E1**2))              #defines r dot for first RK4 step
    
    t_dot_1     = t_dot     + (h/2) * A1
    Theta_dot_1 = Theta_dot + (h/2) * B1
    r_dot_1     = r_dot     + (h/2) * C1
    t_1         = t         + (h/2) * D1
    Theta_1     = Theta     + (h/2) * E1
    r_1         = r         + (h/2) * F1
    S_1           = S         + (h/2)
    
    A2 = 2*(Q**2-M*r_1) * r_dot_1*t_dot_1 / (r_1**2-2*M*r_1+Q**2)                                                   
    B2 = -2*Theta_dot_1*r_dot_1 / r_1                                                                               
    C2 = (r_1-2*M*r_1+Q**2)*(Q**2-M*r_1)*t_dot_1**2 / r_1**5 + (M*r_1-Q**2)*r_dot_1**2 / (r_1**2-2*M*r_1+Q**2)      
    D2 = E*r_1**2/(r_1**2-2*M*r_1+Q**2)                                                                                   
    E2 = L/r_1**2                                                                                                     
    F2 = np.sqrt(-(1-r_s/r_1+Q**2/r_1**2) * (1-(1-r_s/r_1+Q**2/r_1**2)*D2**2 + r_1**2*E2**2))                                 
    
    t_dot_2     = t_dot     + (h/2) * A2
    Theta_dot_2 = Theta_dot + (h/2) * B2
    r_dot_2     = r_dot     + (h/2) * C2
    t_2         = t         + (h/2) * D2
    Theta_2     = Theta     + (h/2) * E2
    r_2         = r         + (h/2) * F2
    S_2           = S         + (h/2)
    
    
    A3 = 2*(Q**2-M*r_2) * r_dot_2*t_dot_2 / (r_2**2-2*M*r_2+Q**2)                                                   
    B3 = -2*Theta_dot_2*r_dot_2 / r_2                                                                               
    C3 = (r_2-2*M*r_2+Q**2)*(Q**2-M*r_2)*t_dot_2**2 / r_2**5 + (M*r_2-Q**2)*r_dot_2**2 / (r_2**2-2*M*r_2+Q**2)      
    D3 = E*r_2**2/(r_2**2-2*M*r_2+Q**2)                                                                                   
    E3 = L/r_2**2                                                                                                     
    F3 = np.sqrt(-(1-r_s/r_2+Q**2/r_2**2) * (1-(1-r_s/r_2+Q**2/r_2**2)*D3**2 + r_2**2*E3**2))                                 
    
    t_dot_3     = t_dot     + (h/2) * A3
    Theta_dot_3 = Theta_dot + (h/2) * B3
    r_dot_3     = r_dot     + (h/2) * C3
    t_3         = t         + (h/2) * D3
    Theta_3     = Theta     + (h/2) * E3
    r_3         = r         + (h/2) * F3 
    S_3           = S       + (h/2)
    
    
    
    A4 = 2*(Q**2-M*r_3) * r_dot_3*t_dot_3 / (r_3**2-2*M*r_3+Q**2)                                                   
    B4 = -2*Theta_dot_3*r_dot_3 / r_3                                                                              
    C4 = (r_3-2*M*r_3+Q**2)*(Q**2-M*r_3)*t_dot_3**2 / r_3**5 + (M*r_3-Q**2)*r_dot_3**2 / (r_3**2-2*M*r_3+Q**2)      
    D4 = E*r_3**2/(r_3**2-2*M*r_3+Q**2)                                                                                   
    E4 = L/r_3**2                                                                                                     
    F4 = np.sqrt(-(1-r_s/r_3+Q**2/r_3**2) * (1-(1-r_s/r_3+Q**2/r_3**2)*D3**2 + r_3**2*E3**2))                                #defines r dot for first RK4 step
    
    t_dot     = t_dot     + (h/6.0) * (A1+(2.*A2)+(2.0*A3) + A4)
    Theta_dot = Theta_dot + (h/6.0) * (B1+(2.*B2)+(2.0*B3) + B4)
    r_dot     =  r_dot    + (h/6.0) * (C1+(2.*C2)+(2.0*C3) + C4)
    t         =  t        + (h/6.0) * (D1+(2.*D2)+(2.0*D3) + D4)
    Theta     = Theta     + (h/6.0) * (E1+(2.*E2)+(2.0*E3) + E4)
    r         =  r        + (h/6.0) * (F1+(2.*F2)+(2.0*F3) + F4)
    S          = S+h
    
    print(L,r**2*Theta_dot)
    
    
    plt.axes(projection = 'polar')
    plt.polar(Theta, r, 'g.')

Take the three second order differential equations you have provided.取您提供的三个二阶微分方程。 These are the geodesic equations parametrized by proper time.这些是由适当时间参数化的测地线方程。 You original metric however is rotationally invariant (ie SO(3) invariant), so it has a set of simple conservation laws, plus the conservation of the metric (ie conservation of proper-time).但是,您的原始度量是旋转不变的(即 SO(3) 不变),因此它具有一组简单的守恒定律,以及度量的守恒(即适当时间的守恒)。 This means that the second order differential equations for t and theta can be integrated once, leading to a set of two first order differential equations for t and theta and one second order differential equation for r :这意味着ttheta的二阶微分方程可以积分一次,从而得到一组ttheta的两个一阶微分方程和一个r的二阶微分方程:

dt/ds = c_0 * r**2 / (r**2 - 2*M*r + Q**2)

dtheta/ds = c_1 / r**2

d**2r/ds**2 = ( (r**2-2*M*r + Q**2)*(Q**2 - M*r)/r**5) * (dt/ds)**2 
                   + ( (M*r - Q**2) /(r**2 - 2*M*r + Q**2) ) * (dr/ds)**2

You can go different ways here, one of them is deriving and equation of a first order differential equation of motion for r by substituting the first two equations above into the equation that the metric evaluated on the trajectory is equal to 1. But you can also just go directly here and plug the right-hand side of the equation of dt/ds into the third equation for r , expressing the system as您可以在这里使用不同的方式 go,其中一种是通过将上面的前两个方程代入轨道上评估的度量等于 1 的方程,推导r的一阶微分运动方程和方程。但您也可以只是 go 直接在这里并将dt/ds方程的右侧代入r的第三个方程,将系统表示为

dt/ds = c_0 * r**2 / (r**2 - 2*M*r + Q**2)

dtheta/ds = c_1 / r**2

d**2r/ds**2 = ( c_0**2*(Q**2 - M*r)/(r*(r**2-2*M*r + Q**2)))
                + ( (M*r - Q**2) /(r**2 - 2*M*r + Q**2) ) * (dr/ds)**2

and to avoid using square roots and complications (square roots are also expensive computations, while rational functions are simple faster algebraic computations), define the equivalent system of four first-order differential equations并且为了避免使用平方根和复杂性(平方根也是昂贵的计算,而有理函数是简单的更快的代数计算),定义四个一阶微分方程的等效系统

dt/ds = c_0 * r**2 / (r**2 - 2*M*r + Q**2)

dtheta/ds = c_1 / r**2

dr/ds = u

du/ds = ( c_0**2*(Q**2 - M*r)/(r*(r**2-2*M*r + Q**2)))
                + ( (M*r - Q**2) /(r**2 - 2*M*r + Q**2) ) * u**2

With the help of the initial conditions for t, theta, r and their derivatives dt/dt, dtheta/dt, dr/dt you can compute the constants c_0 and c_1 used in the first and second equation and then compute the initial condition for u = dr/dt .借助t, theta, r及其导数dt/dt, dtheta/dt, dr/dt的初始条件,您可以计算第一个和第二个等式中使用的常数c_0c_1 ,然后计算u = dr/dt的初始条件u = dr/dt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM