8.4 求微分方程组的数值解 x'=-x^3-y,x(0)=1,y'=x-y^3,y(0)=0.5,0<=t<=30,要求画出x(t)和y(t)的解曲线图形,再相平面上画出轨线

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

def system(t, state):
x, y = state
dxdt = -x3 - y
dydt = x - y
3
return [dxdt, dydt]

t_span = (0, 30)
y0 = [1, 0.5]

sol = solve_ivp(system, t_span, y0, t_eval=np.linspace(t_span[0], t_span[1], 1000))

x = sol.y[0]
y = sol.y[1]
t = sol.t

plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.plot(t, x, label='x(t)')
plt.xlabel('t')
plt.ylabel('x(t)')
plt.title('x(t) vs t')
plt.legend()
plt.grid(True)

plt.subplot(1, 2, 2)
plt.plot(t, y, label='y(t)')
plt.xlabel('t')
plt.ylabel('y(t)')
plt.title('y(t) vs t')
plt.legend()
plt.grid(True)

plt.figure(figsize=(6, 6))
plt.plot(x, y, label='Phase Trajectory')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Phase Plane (x, y)')
plt.legend()
plt.grid(True)
plt.axis('equal')

plt.tight_layout()
plt.show()

print("学号后两位:04")


posted on   黄元元  阅读(41)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示