用Python耦合微分方程

2024-04-19 20:20:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用python解一个测地线轨道方程组。它们是耦合的常微分方程。我尝试了不同的方法,但它们都给了我一个错误的形状(在绘制r和phi时,形状应该是一些周期函数)。你知道怎么做吗? 这是我的常数

G = 4.30091252525 * (pow(10, -3)) #Gravitational constant in (parsec*km^2)/(Ms*sec^2)
c = 0.0020053761 #speed of light , AU/sec
M = 170000 #mass of the central body, in solar masses
m = 10 #mass of the orbiting body, in solar masses
rs = 2 * G * M / pow(c, 2) #Schwarzschild radius
Lz= 0.000024 #Angular momemntum
h = Lz / m #Just the constant  in equation
E= 1.715488e-007 #energy

初始条件为:

Y(0) = rs
Phi(0) = math.pi

轨道方程

image

我尝试这样做的方式:

def rhs(t, u):
    Y, phi = u
    dY = np.sqrt((E**2 / (m**2 * c**2) - (1 - rs / Y) * (c**2 + h**2 / Y**2)))
    dphi = L / Y**2
    return [dY, dphi]

Y0 = np.array([rs,math.pi])
sol = solve_ivp(rhs, [1, 1000], Y0, method='Radau', dense_output=True)

Tags: oftheinbodysecmass形状phi
1条回答
网友
1楼 · 发布于 2024-04-19 20:20:05

看起来你在看一个物体在史瓦西引力中运动的测地线方程的不变平面上的空间坐标

可以使用许多不同的方法,尽可能多地保留模型的基本几何结构,如辛几何积分器或微扰理论。正如Lutz Lehmann在评论中指出的,“solve_ivp”的默认方法使用休眠Prince(4)5步进器作为默认方法,该步进器采用外推模式,即5阶步进,步长选择由4阶步进的误差估计驱动

警告:Y的初始条件等于Schwarzschild半径,因此这些方程可能会失败或需要特殊处理(特别是方程的时间成分,此处未包括!)可能是您必须切换到不同的坐标,以消除偶数地平线上的奇异性。此外,解可能不是周期曲线,而是准周期的,因此它们可能无法很好地闭合

对于快速而肮脏的治疗,但可能是相当准确的治疗,我将区分第一个方程

(dr / dtau)^2 = (E2_mc2 - c2) + (2*GM)/r - (h^2)/(r^2) + (r_schw*h^2)/(r^3)

关于适当的时间tau,然后抵消两边关于r的一阶导数dr / dtau,最后得到左边半径r的二阶导数方程。然后将这个二阶导数方程转化为r及其变化率v的一对一阶导数方程,即

dphi / dtau = h / (r^2)
  dr / dtau = v
  dv / dtau = - GM / (r^2) + h^2 / (r^3) - 3*r_schw*(h^2) / (2*r^4)

并从{}的原始方程及其一阶导数{}计算变化率{}的初始值,即,我将用{}解{}方程:

(v0)^2 = (E2_mc2 - c2) + (2*GM)/r0 - (h^2)/(r0^2) + (r_schw*h^2)/(r0^3)

也许像这样的python代码可以工作:

import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
#from ode_helpers import state_plotter

# u = [phi, Y, V, t] or if time is excluded 
# u = [phi, Y, V]
def f(tau, u, param):
    E2_mc2, c2, GM, h, r_schw = param
    Y = u[1]
    f_phi = h / (Y**2)
    f_Y = u[2] # this is the dr / dt auxiliary equation
    f_V =  - GM / (Y**2) + h**2 / (Y**3) - 3*r_schw*(h**2) / (2*Y**4)
    #f_time = (E2_mc2 * Y) / (Y - r_schw) # this is the equation of the time coordinate
    return [f_phi, f_Y, f_V] # or [f_phi, f_Y, f_V, f_time] 

# from the initial value for r = Y0 and given energy E,  
# calculate the initial rate of change dr / dtau = V0
def ivp(Y0, param, sign):
    E2_mc2, c2, GM, h, r_schw = param
    V0 = math.sqrt((E2_mc2 - c2) + (2*GM)/Y0 - (h**2)/(Y0**2) + (r_schw*h**2)/(Y0**3))
    return sign*V0

G = 4.30091252525 * (pow(10, -3)) #Gravitational constant in (parsec*km^2)/(Ms*sec^2)
c = 0.0020053761 #speed of light , AU/sec
M = 170000 #mass of the central body, in solar masses
m = 10 #mass of the orbiting body, in solar masses
Lz= 0.000024 #Angular momemntum
h = Lz / m #Just the constant  in equation
E= 1.715488e-007 #energy

c2 = c**2
E2_mc2 = (E**2) / (c2*m**2)
GM = G*M
r_schw = 2*GM / c2

param = [E2_mc2, c2, GM, h, r_schw]
Y0 = r_schw
sign = 1 # or -1
V0 = ivp(Y0, param, sign)

tau_span = np.linspace(1, 1000, num=1000)
u0 = [math.pi, Y0, V0]
    
sol = solve_ivp(lambda tau, u: f(tau, u, param), [1, 1000], u0, t_eval=tau_span)

仔细检查方程式,可能出现错误和不准确

相关问题 更多 >