260109-test
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 122 KiB |
@@ -0,0 +1,90 @@
|
||||
# /// script
|
||||
# requires-python = ">=3.11"
|
||||
# dependencies = ["numpy", "matplotlib"]
|
||||
# ///
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# 设置字体
|
||||
plt.rcParams['font.sans-serif'] = ['WenQuanYi Micro Hei', 'Noto Sans CJK SC', 'Microsoft YaHei', 'SimHei', 'SimSun', 'DejaVu Sans']
|
||||
plt.rcParams['axes.unicode_minus'] = False
|
||||
|
||||
# 创建图形
|
||||
fig, ax = plt.subplots(figsize=(10, 8), dpi=150)
|
||||
|
||||
# 定义函数
|
||||
def f(x):
|
||||
return -2*x**2 + 8*x - 3
|
||||
|
||||
# 生成 x 数据(根据顶点位置设置合理范围)
|
||||
x = np.linspace(-1, 5, 500)
|
||||
y = f(x)
|
||||
|
||||
# 绘制函数曲线
|
||||
ax.plot(x, y, 'b-', linewidth=2.5, label=r'$y = -2x^2 + 8x - 3$')
|
||||
|
||||
# 标记顶点
|
||||
vertex_x, vertex_y = 2, 5
|
||||
ax.scatter([vertex_x], [vertex_y], color='red', s=120, zorder=5, edgecolors='darkred', linewidths=2)
|
||||
ax.annotate(f'顶点 ({vertex_x}, {vertex_y})\n最大值',
|
||||
xy=(vertex_x, vertex_y),
|
||||
xytext=(vertex_x + 0.8, vertex_y + 0.5),
|
||||
fontsize=12,
|
||||
ha='left',
|
||||
arrowprops=dict(arrowstyle='->', color='red', lw=1.5),
|
||||
bbox=dict(boxstyle='round,pad=0.3', facecolor='lightyellow', edgecolor='orange'))
|
||||
|
||||
# 标记与 x 轴的交点(求根)
|
||||
# -2x² + 8x - 3 = 0 → x = (8 ± √(64-24))/(-4) = (8 ± √40)/(-4)
|
||||
discriminant = 64 - 24
|
||||
x1 = (8 - np.sqrt(discriminant)) / 4
|
||||
x2 = (8 + np.sqrt(discriminant)) / 4
|
||||
ax.scatter([x1, x2], [0, 0], color='green', s=100, zorder=5, marker='s', edgecolors='darkgreen', linewidths=2)
|
||||
ax.annotate(f'x ≈ {x1:.2f}', xy=(x1, 0), xytext=(x1 - 0.3, -1.5), fontsize=10, ha='center',
|
||||
arrowprops=dict(arrowstyle='->', color='green', lw=1))
|
||||
ax.annotate(f'x ≈ {x2:.2f}', xy=(x2, 0), xytext=(x2 + 0.3, -1.5), fontsize=10, ha='center',
|
||||
arrowprops=dict(arrowstyle='->', color='green', lw=1))
|
||||
|
||||
# 绘制对称轴
|
||||
ax.axvline(x=vertex_x, color='purple', linestyle='--', linewidth=1.5, alpha=0.7, label=f'对称轴 x = {vertex_x}')
|
||||
|
||||
# 绘制参考线
|
||||
ax.axhline(y=0, color='gray', linestyle='-', linewidth=0.8, alpha=0.5)
|
||||
ax.axhline(y=vertex_y, color='orange', linestyle=':', linewidth=1.5, alpha=0.7, label=f'最大值 y = {vertex_y}')
|
||||
|
||||
# 填充顶点到 x 轴的区域(可视化最大值)
|
||||
x_fill = np.linspace(x1, x2, 100)
|
||||
y_fill = f(x_fill)
|
||||
ax.fill_between(x_fill, y_fill, 0, alpha=0.15, color='blue', label='函数值 > 0 的区域')
|
||||
|
||||
# 设置坐标轴
|
||||
ax.set_xlim(-1, 5)
|
||||
ax.set_ylim(-4, 7)
|
||||
ax.set_xlabel('x', fontsize=14)
|
||||
ax.set_ylabel('y', fontsize=14)
|
||||
ax.set_title(r'二次函数 $y = -2x^2 + 8x - 3$ 图像', fontsize=16, fontweight='bold')
|
||||
|
||||
# 添加网格
|
||||
ax.grid(True, linestyle='--', alpha=0.4)
|
||||
|
||||
# 添加图例
|
||||
ax.legend(loc='lower right', fontsize=11, framealpha=0.9)
|
||||
|
||||
# 添加说明文字框
|
||||
textstr = '\n'.join([
|
||||
'关键信息:',
|
||||
f'• 顶点坐标: ({vertex_x}, {vertex_y})',
|
||||
f'• 最大值: {vertex_y}',
|
||||
f'• 开口方向: 向下 (a < 0)',
|
||||
f'• 对称轴: x = {vertex_x}'
|
||||
])
|
||||
props = dict(boxstyle='round', facecolor='white', edgecolor='gray', alpha=0.9)
|
||||
ax.text(0.02, 0.98, textstr, transform=ax.transAxes, fontsize=11,
|
||||
verticalalignment='top', bbox=props)
|
||||
|
||||
plt.tight_layout()
|
||||
plt.savefig('figure.png', bbox_inches='tight', facecolor='white')
|
||||
plt.close()
|
||||
|
||||
print("图像已保存: figure.png")
|
||||
@@ -0,0 +1,116 @@
|
||||
# 二次函数 $y = -2x^2 + 8x - 3$ 顶点与最值 - 求解报告
|
||||
|
||||
## 1. 🎯 问题描述
|
||||
|
||||
已知二次函数 $y = -2x^2 + 8x - 3$,求:
|
||||
|
||||
1. 函数的顶点坐标
|
||||
2. 函数的最大值
|
||||
|
||||
## 2. ✅ 最终结论
|
||||
|
||||
对于二次函数 $y = -2x^2 + 8x - 3$:
|
||||
|
||||
**顶点坐标为 $(2, 5)$**。由于二次项系数 $a = -2 < 0$,抛物线开口向下,因此函数在顶点处取得**最大值 $y_{max} = 5$**,此时 $x = 2$。
|
||||
|
||||
换句话说,当 $x = 2$ 时,函数值达到最大,为 $5$;当 $x$ 偏离 $2$ 时(无论向左还是向右),函数值都会减小。
|
||||
|
||||
## 3. 📈 可视化
|
||||
|
||||

|
||||
|
||||
**图表说明**:
|
||||
|
||||
- **蓝色曲线**:二次函数 $y = -2x^2 + 8x - 3$ 的图像
|
||||
- **红色圆点**:顶点 $(2, 5)$,即函数的最高点
|
||||
- **绿色方块**:函数与 $x$ 轴的两个交点(零点)
|
||||
- **紫色虚线**:对称轴 $x = 2$
|
||||
- **橙色点线**:最大值参考线 $y = 5$
|
||||
- **浅蓝色区域**:函数值大于零的区域
|
||||
|
||||
## 4. 🧠 数学建模与解题过程
|
||||
|
||||
<details>
|
||||
<summary><strong>点击展开</strong></summary>
|
||||
|
||||
**问题分析**:这是一个关于二次函数顶点和最值的基本问题。对于一般形式的二次函数 $y = ax^2 + bx + c$,其图像是一条抛物线,顶点坐标和最值可以通过多种方法求解。
|
||||
|
||||
**方法选择**:本题采用三种方法相互验证:
|
||||
|
||||
### 方法一:顶点公式
|
||||
|
||||
对于 $y = ax^2 + bx + c$,顶点坐标为:
|
||||
|
||||
$$\left( -\frac{b}{2a}, \frac{4ac - b^2}{4a} \right)$$
|
||||
|
||||
本题中 $a = -2$,$b = 8$,$c = -3$,代入得:
|
||||
|
||||
$$x_{顶点} = -\frac{8}{2 \times (-2)} = -\frac{8}{-4} = 2$$
|
||||
|
||||
$$y_{顶点} = \frac{4 \times (-2) \times (-3) - 8^2}{4 \times (-2)} = \frac{24 - 64}{-8} = \frac{-40}{-8} = 5$$
|
||||
|
||||
### 方法二:求导法
|
||||
|
||||
对函数求导:
|
||||
|
||||
$$y' = \frac{d}{dx}(-2x^2 + 8x - 3) = -4x + 8$$
|
||||
|
||||
令 $y' = 0$,解得 $x = 2$。
|
||||
|
||||
将 $x = 2$ 代入原函数:$y = -2(2)^2 + 8(2) - 3 = -8 + 16 - 3 = 5$
|
||||
|
||||
### 方法三:配方法
|
||||
|
||||
$$y = -2x^2 + 8x - 3$$
|
||||
|
||||
$$= -2(x^2 - 4x) - 3$$
|
||||
|
||||
$$= -2(x^2 - 4x + 4 - 4) - 3$$
|
||||
|
||||
$$= -2(x - 2)^2 + 8 - 3$$
|
||||
|
||||
$$= -2(x - 2)^2 + 5$$
|
||||
|
||||
顶点式为 $y = -2(x - 2)^2 + 5$,直接读出顶点 $(2, 5)$。
|
||||
|
||||
**结论**:三种方法结果一致,顶点为 $(2, 5)$。由于 $a = -2 < 0$,抛物线开口向下,函数在 $x = 2$ 处取得最大值 $5$。
|
||||
|
||||
</details>
|
||||
|
||||
## 5. 📊 运行结果
|
||||
|
||||
<details>
|
||||
<summary><strong>点击展开</strong></summary>
|
||||
|
||||
```
|
||||
==================================================
|
||||
二次函数 y = -2x² + 8x - 3 求解
|
||||
==================================================
|
||||
|
||||
【方法1:顶点公式】
|
||||
a = -2, b = 8, c = -3
|
||||
顶点横坐标 x = -b/(2a) = -8/(2×-2) = 2.0
|
||||
顶点纵坐标 y = (4ac-b²)/(4a) = 5.0
|
||||
|
||||
【方法2:SymPy 求导验证】
|
||||
y' = 8 - 4*x
|
||||
令 y' = 0,解得 x = [2]
|
||||
将 x = 2 代入原函数:y = 5
|
||||
|
||||
【方法3:配方法】
|
||||
y = -2x² + 8x - 3
|
||||
= -2(x² - 4x) - 3
|
||||
= -2(x² - 4x + 4 - 4) - 3
|
||||
= -2(x - 2)² + 8 - 3
|
||||
= -2(x - 2)² + 5
|
||||
顶点形式:y = -2(x - 2)² + 5
|
||||
|
||||
==================================================
|
||||
【最终结果】
|
||||
==================================================
|
||||
顶点坐标:(2, 5)
|
||||
由于 a = -2 < 0,抛物线开口向下
|
||||
函数最大值:y_max = 5(在 x = 2 处取得)
|
||||
```
|
||||
|
||||
</details>
|
||||
@@ -0,0 +1,61 @@
|
||||
# /// script
|
||||
# requires-python = ">=3.11"
|
||||
# dependencies = ["sympy"]
|
||||
# ///
|
||||
|
||||
import sympy as sp
|
||||
|
||||
# 定义符号变量
|
||||
x = sp.symbols('x', real=True)
|
||||
|
||||
# 定义二次函数
|
||||
y = -2*x**2 + 8*x - 3
|
||||
|
||||
print("=" * 50)
|
||||
print("二次函数 y = -2x² + 8x - 3 求解")
|
||||
print("=" * 50)
|
||||
|
||||
# 方法1:使用配方法/顶点公式
|
||||
# 对于 y = ax² + bx + c,顶点为 (-b/(2a), (4ac-b²)/(4a))
|
||||
a, b, c = -2, 8, -3
|
||||
|
||||
# 顶点横坐标
|
||||
x_vertex = -b / (2*a)
|
||||
# 顶点纵坐标(最值)
|
||||
y_vertex = (4*a*c - b**2) / (4*a)
|
||||
|
||||
print(f"\n【方法1:顶点公式】")
|
||||
print(f"a = {a}, b = {b}, c = {c}")
|
||||
print(f"顶点横坐标 x = -b/(2a) = -{b}/(2×{a}) = {x_vertex}")
|
||||
print(f"顶点纵坐标 y = (4ac-b²)/(4a) = {y_vertex}")
|
||||
|
||||
# 方法2:使用 SymPy 求导
|
||||
print(f"\n【方法2:SymPy 求导验证】")
|
||||
dy = sp.diff(y, x)
|
||||
print(f"y' = {dy}")
|
||||
|
||||
# 令导数为0,求驻点
|
||||
critical_points = sp.solve(dy, x)
|
||||
print(f"令 y' = 0,解得 x = {critical_points}")
|
||||
|
||||
if critical_points:
|
||||
x_val = critical_points[0]
|
||||
y_val = y.subs(x, x_val)
|
||||
print(f"将 x = {x_val} 代入原函数:y = {y_val}")
|
||||
|
||||
# 方法3:配方法展示
|
||||
print(f"\n【方法3:配方法】")
|
||||
print("y = -2x² + 8x - 3")
|
||||
print(" = -2(x² - 4x) - 3")
|
||||
print(" = -2(x² - 4x + 4 - 4) - 3")
|
||||
print(" = -2(x - 2)² + 8 - 3")
|
||||
print(" = -2(x - 2)² + 5")
|
||||
print("顶点形式:y = -2(x - 2)² + 5")
|
||||
|
||||
# 最终结果
|
||||
print("\n" + "=" * 50)
|
||||
print("【最终结果】")
|
||||
print("=" * 50)
|
||||
print(f"顶点坐标:({int(x_vertex)}, {int(y_vertex)})")
|
||||
print(f"由于 a = {a} < 0,抛物线开口向下")
|
||||
print(f"函数最大值:y_max = {int(y_vertex)}(在 x = {int(x_vertex)} 处取得)")
|
||||
Reference in New Issue
Block a user