第八章習題

犬塚米灵發表於2024-11-18

習題8.4

import numpy as np
import pandas as pd
import sympy as sp
sp.init_printing(use_latex=True)
from scipy.integrate import odeint
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False   
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
def rhs(f, t):
    x, y = f
    return [
        -x**3 - y,
        x - y**3,
    ]
tt = np.linspace(0, 30, 301)
ns = odeint(rhs, [1, 0.5], tt)
xx, yy = ns[:,0], ns[:,1]

fig = plt.figure(figsize=(6,3))
ax = fig.add_subplot(121)
ax.plot(tt, xx, label='$x(t)$')
ax.plot(tt, yy, label='$y(t)$')
ax.legend()

ax1 = fig.add_subplot(122)
ax1.plot(xx, yy)

fig.show()
print("3035")

習題8.5

import numpy as np
import pandas as pd
import sympy as sp
sp.init_printing(use_latex=True)
from scipy.integrate import odeint
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False   
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
def rhs(f, t):
    f, f1, f2, T, T1 = f
    return [
        f1,
        f2, 
        -3*f*f2 + 2*f1**2 - T,
        T1,
        -2.1*f*T1,
    ]

f0 = np.array([0, 0, 0.68, 1, -0.5])
nn = np.linspace(0, 10, 200)
ns = odeint(rhs, f0, nn)
ff, TT = ns[:,0], ns[:,3]
plt.plot(nn, ff)
plt.plot(nn, TT)
plt.show()
print("3035")

相關文章