|
|

楼主 |
发表于 2026-9-29 08:09:16
|
显示全部楼层
- import numpy as np
- import matplotlib.pyplot as plt
- # ============ 0. 中文字体配置 ============
- plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'Arial Unicode MS']
- plt.rcParams['axes.unicode_minus'] = False
- # ============ 1. 定义原函数 f(x) ============
- def f(x):
- return x * (x+1) * (x+2) * (x+3) * (x+4) * (x+5)
- # ============ 2. 对称换元:t = x + 2.5, u = t^2 ============
- def g(u):
- # g(u) = (u - 6.25)(u - 2.25)(u - 0.25)
- return (u - 6.25) * (u - 2.25) * (u - 0.25)
- # ============ 3. 精确解析解 ============
- # 对 g(u) 求导:g'(u) = 3u^2 - 17.5u + 16.1875 = 0
- # 极小值点 u = (35 + 8*sqrt(7)) / 12
- u_min_exact = (35 + 8*np.sqrt(7)) / 12
- g_min_exact = (u_min_exact - 6.25) * (u_min_exact - 2.25) * (u_min_exact - 0.25)
- t_min = np.sqrt(u_min_exact)
- x1_exact = -2.5 - t_min
- x2_exact = -2.5 + t_min
- print("=" * 55)
- print("【解析解】")
- print("=" * 55)
- print(f"极小值点 u* = (35 + 8√7)/12 ≈ {u_min_exact:.6f}")
- print(f"最小值 g(u*) = (-160 - 112√7)/27 ≈ {g_min_exact:.6f}")
- print(f"对应 t = ±√u* ≈ ±{t_min:.6f}")
- print(f"对应 x1 = -2.5 - √u* ≈ {x1_exact:.6f}")
- print(f"对应 x2 = -2.5 + √u* ≈ {x2_exact:.6f}")
- # ============ 4. 数值验证 ============
- print("\n" + "=" * 55)
- print("【数值验证】")
- print("=" * 55)
- print(f"f(x1) = {f(x1_exact):.8f}")
- print(f"f(x2) = {f(x2_exact):.8f}")
- print(f"f(-2.5) = {f(-2.5):.8f} (对称中心,非最小值)")
- print(f"f(-4.66) = {f(-4.66):.8f} (近似点)")
- print(f"f(-4.67) = {f(-4.67):.8f}")
- print(f"f(-4.65) = {f(-4.65):.8f}")
- # 用 scipy 数值求最小值做交叉验证
- try:
- from scipy.optimize import minimize_scalar
- res = minimize_scalar(f, bounds=(-5, 0), method='bounded')
- print(f"\nscipy 数值最小值: x ≈ {res.x:.6f}, f(x) ≈ {res.fun:.8f}")
- except ImportError:
- print("\n(未安装 scipy,跳过数值优化验证)")
- # ============ 5. 图形 ============
- fig, axes = plt.subplots(1, 2, figsize=(14, 5.5))
- # ---- 图1:原函数 f(x) ----
- x = np.linspace(-6, 1, 2000)
- y = f(x)
- ax1 = axes[0]
- ax1.plot(x, y, color='blue', linewidth=2, label=r'$f(x)=x(x+1)(x+2)(x+3)(x+4)(x+5)$')
- ax1.axhline(0, color='black', linewidth=0.8, linestyle='--')
- ax1.axvline(-2.5, color='gray', linewidth=0.8, linestyle=':', label='x = -2.5 (对称中心)')
- # 标出整数根
- for r in [-5, -4, -3, -2, -1, 0]:
- ax1.plot(r, 0, 'ro', markersize=5)
- ax1.text(r, 3, f'{r}', ha='center', color='red', fontsize=9)
- # 标出最小值点
- ax1.plot(x1_exact, g_min_exact, 'mo', markersize=10, zorder=5)
- ax1.annotate(f'最小值点\n({x1_exact:.3f}, {g_min_exact:.3f})',
- xy=(x1_exact, g_min_exact),
- xytext=(x1_exact + 0.8, g_min_exact - 6),
- fontsize=10, color='purple',
- arrowprops=dict(arrowstyle='->', color='purple'))
- ax1.plot(x2_exact, g_min_exact, 'mo', markersize=10, zorder=5)
- ax1.annotate(f'({x2_exact:.3f}, {g_min_exact:.3f})',
- xy=(x2_exact, g_min_exact),
- xytext=(x2_exact + 0.3, g_min_exact - 8),
- fontsize=9, color='purple',
- arrowprops=dict(arrowstyle='->', color='purple'))
- ax1.set_xlabel('x', fontsize=12)
- ax1.set_ylabel('f(x)', fontsize=12)
- ax1.set_title('原函数 f(x) 的图像', fontsize=13)
- ax1.set_ylim(-25, 15)
- ax1.legend(fontsize=9)
- ax1.grid(True, alpha=0.3)
- # ---- 图2:化简后的三次函数 g(u) ----
- u = np.linspace(0, 8, 1000)
- gu = g(u)
- ax2 = axes[1]
- ax2.plot(u, gu, color='darkorange', linewidth=2, label=r'$g(u)=(u-6.25)(u-2.25)(u-0.25)$')
- ax2.axhline(0, color='black', linewidth=0.8, linestyle='--')
- # 三个根
- for r in [0.25, 2.25, 6.25]:
- ax2.plot(r, 0, 'ro', markersize=6)
- ax2.text(r, 1.5, f'{r}', ha='center', color='red', fontsize=9)
- # 全局最小值
- ax2.plot(u_min_exact, g_min_exact, 'mo', markersize=10, zorder=5)
- ax2.annotate(f'全局最小\n(u*≈{u_min_exact:.3f}, {g_min_exact:.3f})',
- xy=(u_min_exact, g_min_exact),
- xytext=(u_min_exact + 0.5, g_min_exact + 3),
- fontsize=10, color='purple',
- arrowprops=dict(arrowstyle='->', color='purple'))
- # 局部极大值(数值找)
- mask = (u > 0.25) & (u < 2.25)
- idx_max = np.argmax(gu[mask])
- u_max = u[mask][idx_max]
- g_max = gu[mask][idx_max]
- ax2.plot(u_max, g_max, 'go', markersize=8)
- ax2.annotate(f'局部极大\n({u_max:.3f}, {g_max:.3f})',
- xy=(u_max, g_max),
- xytext=(u_max + 0.8, g_max + 2),
- fontsize=9, color='green',
- arrowprops=dict(arrowstyle='->', color='green'))
- ax2.set_xlabel('u = (x + 2.5)²', fontsize=12)
- ax2.set_ylabel('g(u)', fontsize=12)
- ax2.set_title('化简后的三次函数 g(u) = (u-6.25)(u-2.25)(u-0.25)', fontsize=12)
- ax2.legend(fontsize=9)
- ax2.grid(True, alpha=0.3)
- plt.tight_layout()
- plt.savefig('min_solution.png', dpi=150)
- plt.show()
- print("\n图形已保存为 min_solution.png")
复制代码 |
|