习题8.4

1.代码实现

点击查看代码
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt


# 定义微分方程组
def system(state, t):
    x, y = state
    dxdt = -x ** 3 - y
    dydt = x - y ** 3
    return [dxdt, dydt]


# 初始条件
x0 = 1
y0 = 0.5
state0 = [x0, y0]

# 时间范围
t = np.linspace(0, 30, 1000)

# 求解微分方程组
solution = odeint(system, state0, t)
x_sol = solution[:, 0]
y_sol = solution[:, 1]

# 绘制解曲线
plt.figure()
plt.plot(t, x_sol, label='x(t)')
plt.plot(t, y_sol, label='y(t)')
plt.xlabel('t')
plt.ylabel('x, y')
plt.title('Solution of the system of differential equations')
print("3014")
plt.legend()
plt.show()

# 绘制相平面轨迹
plt.figure()
plt.plot(x_sol, y_sol)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Phase plane trajectory')
plt.show()

2.运行结果

posted on 2024-11-17 15:21  克卜勒星球  阅读(2)  评论(0编辑  收藏  举报

导航