简体   繁体   English

将一组二阶微分方程与Scipy集成

[英]Integrating a set of second order differential equations with Scipy

I have a set of second order differential equations: 我有一组二阶微分方程:

在此处输入图片说明

and I would like to solve for 我想解决 在此处输入图片说明 using odeint in python. 在python中使用odeint Is this possible? 这可能吗?

I have attempted to do it, but my code does not give me the expected result. 我尝试这样做,但是我的代码没有给我预期的结果。

import numpy as np, matplotlib.pyplot as plt, matplotlib.font_manager as fm,os
from scipy.integrate import odeint
from mpl_toolkits.mplot3d.axes3d import Axes3D

initial_state = [0.1, 0, 0]
a = 4
time_points = np.linspace(0, 100, 10000)

def my_system(current_state, t):
    theta1, theta2, theta3 = current_state

    d2theta1_dt2 = -a*(2*theta1-theta2-theta3)
    d2theta2_dt2 = -a*(2*theta2-theta1-theta3)
    d2theta3_dt2 = -a*(2*theta3-theta1-theta2)

    return [d2theta1_dt2, d2theta2_dt2, d2theta3_dt2]

xyz = odeint(my_system, initial_state, time_points)

theta1 = xyz[:, 0]
theta2 = xyz[:, 1]
theta3 = xyz[:, 2]

fig = plt.figure(figsize=(12, 9))
ax = fig.gca(projection='3d')
ax.xaxis.set_pane_color((1,1,1,1))
ax.yaxis.set_pane_color((1,1,1,1))
ax.zaxis.set_pane_color((1,1,1,1))
ax.plot(theta1, theta2, theta3, color='g', alpha=0.7, linewidth=0.6)
ax.set_title('plot', fontproperties=title_font)
plt.show()

Actually, the fix to insert the first order derivatives is rather quick, 实际上,插入一阶导数的修复非常快,

def my_system(current_state, t):
    theta1, theta2, theta3, omega1, omega2, omega3 = current_state

    d2theta1_dt2 = -a*(2*theta1-theta2-theta3)
    d2theta2_dt2 = -a*(2*theta2-theta1-theta3)
    d2theta3_dt2 = -a*(2*theta3-theta1-theta2)

    return [ omega1, omega2, omega3, d2theta1_dt2, d2theta2_dt2, d2theta3_dt2]

where the first three entries of the state are again the angles, and the second three are their velocities. 状态的前三个条目再次是角度,后三个是它们的速度。


You will also need to add initial values for the angular velocities to the initial state vector. 您还需要将角速度的初始值添加到初始状态向量。

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

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