Python:求解自治二阶 ODE

发布于 2025-01-12 15:10:08 字数 565 浏览 0 评论 0原文

我有以下自治常微分方程系统,其中xv是向量(初始条件x0>v0)

dx/dt = v 
dv/dt = F(x, v)

等价地,可以将其编写为二阶 ODE 系统

d^2 x/ dt^2 = F(x, dx/dt)

我如何使用 Python 来解决这个问题?

from scipy.integrate import solve_ivp, odeint

# Suppose F gives me the right-hand side of the ODE or d^2 x /dt^2
F = lambda x, v: pass

# How can I integrate this ODE system numerically?
# For instance from time `t0` to time `t1` 
# Given initial condition `z0 = (x0, v0)`

I have the following autonomous system of ODEs where x and v are vectors (with initial conditions x0 and v0)

dx/dt = v 
dv/dt = F(x, v)

Equivalently, one can write it as a second order ODE system

d^2 x/ dt^2 = F(x, dx/dt)

How can I solve this using Python?

from scipy.integrate import solve_ivp, odeint

# Suppose F gives me the right-hand side of the ODE or d^2 x /dt^2
F = lambda x, v: pass

# How can I integrate this ODE system numerically?
# For instance from time `t0` to time `t1` 
# Given initial condition `z0 = (x0, v0)`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹微笑 2025-01-19 15:10:08

这是我有时使用的复制粘贴模板。假设您有 def F(x, v,parameters): return # some np.array of Accelerations

import numpy as np
from scipy.integrate import solve_ivp

def f(t, y, parameters):
    n = int(len(y)/2)
    x = y[0:n]
    v = y[n:]
    return np.concatenate( ( v,  F(x, v, parameters) ))

t_start = # something
t_step = # something 
n_steps = # something
t_stop = n_steps * t_step


t_nodes = np.linspace(t_start, t_stop, n_steps)
y_initial = # initial positions and velocities
parameters = # parameters of the model

solution = solve_ivp( fun = lambda t, y :  f(t, y, parameters), 
                      t_span = [t_start, t_stop], 
                      y0 = y_initial, 
                      t_eval = t_nodes, 
                      method = 'RK45' )

输出为:

solution.y = # np.array of shape (2*dim, n_steps)
solution.t = # np.array of shape (n_steps,)
positions = solution.y[ 0: int(solution.y.shape[0]/2) ] # np.array of shape (dim, n_steps)
velocities = solution.y[ int(solution.y.shape[0]/2): -1] # np.array of shape (dim, n_steps)

Here is my copy-paste templet that I sometimes use. Assume you have def F(x, v, parameters): return # some np.array of accelerations

import numpy as np
from scipy.integrate import solve_ivp

def f(t, y, parameters):
    n = int(len(y)/2)
    x = y[0:n]
    v = y[n:]
    return np.concatenate( ( v,  F(x, v, parameters) ))

t_start = # something
t_step = # something 
n_steps = # something
t_stop = n_steps * t_step


t_nodes = np.linspace(t_start, t_stop, n_steps)
y_initial = # initial positions and velocities
parameters = # parameters of the model

solution = solve_ivp( fun = lambda t, y :  f(t, y, parameters), 
                      t_span = [t_start, t_stop], 
                      y0 = y_initial, 
                      t_eval = t_nodes, 
                      method = 'RK45' )

The output is:

solution.y = # np.array of shape (2*dim, n_steps)
solution.t = # np.array of shape (n_steps,)
positions = solution.y[ 0: int(solution.y.shape[0]/2) ] # np.array of shape (dim, n_steps)
velocities = solution.y[ int(solution.y.shape[0]/2): -1] # np.array of shape (dim, n_steps)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文