8.4
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def system(z,t):
x,y=z
dxdt=-x3-y
dydt=x-y3
return [dxdt,dydt]
z0=[1,0.5]
t = np.linspace(0, 30, 1000)
sol=odeint(system,z0,t)
x_sol=sol[:,0]
y_sol=sol[:,1]
plt.subplot(2, 1, 1)
plt.plot(t, x_sol)
plt.xlabel('t')
plt.ylabel('x(t)')
plt.title('Solution of x(t)')
plt.subplot(2, 1, 2)
plt.plot(t, y_sol)
plt.xlabel('t')
plt.ylabel('y(t)')
plt.title('Solution of y(t)')
plt.tight_layout()
plt.show()
plt.plot(x_sol, y_sol)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Phase Plane Trajectory')
plt.show()
print("3023")
结果
8.5
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
plt.rcParams['text.usetex'] = False
def model(t, y):
f, df_dm, d2f_dm2, T, dT_dm = y
d3f_dm3 = -3 * f * d2f_dm2 + 2 * (df_dm) ** 2 - T
d2T_dm2 = -2.1 * f * dT_dm
return [df_dm, d2f_dm2, d3f_dm3, dT_dm, d2T_dm2]
y0 = [0, 0, 0.68, 1, -0.5]
t_span = (0, 10)
t_eval = np.linspace(t_span[0], t_span[1], 1000)
try:
sol = solve_ivp(model, t_span, y0, t_eval=t_eval, method='RK45')
if not sol.success:
raise ValueError(f"求解微分方程组失败: {sol.message}")
except Exception as e:
print(f"发生错误: {e}")
exit(1)
f = sol.y[0]
T = sol.y[3]
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
plt.plot(sol.t, f, label='f(m)')
plt.xlabel('m (independent variable)')
plt.ylabel('f(m) (dependent variable)')
plt.title('Solution for f(m) over time')
plt.grid(True)
plt.subplot(2, 1, 2)
plt.plot(sol.t, T, label='T(m)', color='orange')
plt.xlabel('m (independent variable)')
plt.ylabel('T(m) (dependent variable)')
plt.title('Solution for T(m) over time')
plt.grid(True)
plt.tight_layout()
plt.show()
print("3023")
结果