首页 > 其他分享 >习题8.4

习题8.4

时间:2024-11-17 15:07:23浏览次数:1  
标签:plot plt sol 8.4 system solution import 习题

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.运行结果

标签:plot,plt,sol,8.4,system,solution,import,习题
From: https://www.cnblogs.com/qishanhsa/p/18550593

相关文章

  • 数学建模习题7.10
    `importnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltfromscipy.interpolateimportinterp1d,PchipInterpolator,CubicSplinefromscipy.optimizeimportcurve_fitfromscipy.statsimportnorm读取数据file_path='7.17.xISX'#确保文件......
  • 习题7.10(2)
    1.代码实现点击查看代码importnumpyasnpimportpylabasplt#原始数据点x0=np.array([-2,-1.7,-1.4,-1.1,-0.8,-0.5,-0.2,0.1,0.4,0.7,1,1.3,1.6,1.9,2.2,2.5,2.8,3.1,3.4,3.7,4,4.3,4.6,4.9])y0=np.array([0.1029,0.1174,0.1316,0.144......
  • 数学建模习题8.4
    `importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.integrateimportsolve_ivp定义微分方程系统defsystem(t,state):x,y=statedxdt=-x-ydydt=x-y**3return[dxdt,dydt]定义时间跨度和初始条件t_span=(0,30)y0=[1,0.5]解微分方程so......
  • 数学建模习题7.7
    `importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.optimizeimportcurve_fit,leastsq,least_squares定义函数g(x,a,b)defg(x,a,b):return(10*a)/(10*b+(a-10*b)*np.exp(a*np.sin(x)))初始参数a=1.1b=0.01生成数据x_values......
  • 8.4
    importnumpyasnpfromscipy.integrateimportsolve_ivpimportmatplotlib.pyplotaspltdefdifferential_equations(t,z):x,y=zdx_dt=-x**3-ydy_dt=x-y**3return[dx_dt,dy_dt]z0=[1,0.5]t_span=(0,30)sol=solve_ivp(differential_eq......
  • 数学建模习题7.4
    `importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.interpolateimportgriddatadeff(x,y):x2=x2return(x2-2*x)*np.exp(-x2-y2-x*y)x_min,x_max=-3,3y_min,y_max=-4,4num_points=1000x_random=np.random.uniform(x_min,x_ma......
  • 数学建模习题7.3
    `importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.interpolateimportinterp1d,CubicSpline已知的温度和对应的体积T_known=np.array([700,720,740,760,780])V_known=np.array([0.0977,0.1218,0.1406,0.1551,0.1664])需要求解的温度T_query=......
  • 数学建模习题7.1
    `importnumpyasnpimportscipy.interpolateasspiimportscipy.integrateasspi_integrate定义函数g(x)defg(x):return((3x**2+4x+6)*np.sin(x))/(x**2+8*x+6)在区间[0,10]上等间距取1000个点x=np.linspace(0,10,1000)计算这些点处的函数值......
  • 第七章课后习题
    习题7.1点击查看代码importnumpyasnpfromscipy.interpolateimportinterp1dfromscipy.integrateimportquadimportmatplotlib.pyplotaspltg=lambdax:(3*x**2+4*x+6)*np.sin(x)/(x**2+8*x+6)x0=np.linspace(0,10,1000)y0=g(x0......
  • 第八章课后习题
    习题8.4点击查看代码fromscipy.integrateimportodeintimportnumpyasnpimportmatplotlib.pyplotasplt#设置Matplotlib不使用LaTeXplt.rc('font',size=15)plt.rc('text',usetex=False)#定义微分方程系统dz=lambdaz,t:[-z[0]**3-z[1],z[0]-......