20260109_162500_quadratic_vertex_max-工具GithubCopilot模型gemini-claude-opus-4-5-thinking

This commit is contained in:
严浩
2026-01-09 16:26:36 +08:00
parent 9488e4391c
commit 12fd11ce30
4 changed files with 302 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

View File

@@ -0,0 +1,97 @@
# /// 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)
# 定义函数 y = -2x² + 8x - 3
def f(x):
return -2*x**2 + 8*x - 3
# 顶点坐标
x_vertex = 2
y_vertex = 5
# 生成 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$')
# 标注顶点
ax.plot(x_vertex, y_vertex, 'ro', markersize=12, zorder=5, label=f'顶点 ({x_vertex}, {y_vertex})')
ax.annotate(f'顶点\n({x_vertex}, {y_vertex})',
xy=(x_vertex, y_vertex),
xytext=(x_vertex + 0.8, y_vertex + 0.5),
fontsize=12,
ha='left',
arrowprops=dict(arrowstyle='->', color='red', lw=1.5))
# 绘制对称轴
ax.axvline(x=x_vertex, color='green', linestyle='--', linewidth=1.5, alpha=0.7, label=f'对称轴 x = {x_vertex}')
# 绘制最大值水平线
ax.axhline(y=y_vertex, color='orange', linestyle=':', linewidth=1.5, alpha=0.7, label=f'最大值 y = {y_vertex}')
# 绘制坐标轴
ax.axhline(y=0, color='gray', linewidth=0.8)
ax.axvline(x=0, color='gray', linewidth=0.8)
# 求与 x 轴的交点(如果有)
# -2x² + 8x - 3 = 0
# x = (8 ± √(64-24)) / (-4) = (8 ± √40) / (-4)
discriminant = 64 - 24 # b² - 4ac = 64 - 24 = 40
x1 = (8 - np.sqrt(discriminant)) / 4
x2 = (8 + np.sqrt(discriminant)) / 4
ax.plot([x1, x2], [0, 0], 'g^', markersize=10, label=f'与x轴交点')
ax.annotate(f'({x1:.2f}, 0)', xy=(x1, 0), xytext=(x1-0.3, -1.5), fontsize=10, ha='center')
ax.annotate(f'({x2:.2f}, 0)', xy=(x2, 0), xytext=(x2+0.3, -1.5), fontsize=10, ha='center')
# 与 y 轴的交点
y_intercept = f(0)
ax.plot(0, y_intercept, 'ms', markersize=10, label=f'与y轴交点 (0, {y_intercept})')
# 设置坐标轴范围
ax.set_xlim(-1.5, 5.5)
ax.set_ylim(-5, 7)
# 设置网格
ax.grid(True, linestyle='--', alpha=0.5)
# 设置标签和标题
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.legend(loc='lower right', fontsize=11)
# 添加关键信息文本框
textstr = '\n'.join([
r'$y = -2x^2 + 8x - 3$',
r'$= -2(x-2)^2 + 5$',
'',
f'顶点: (2, 5)',
f'最大值: 5',
f'对称轴: x = 2'
])
props = dict(boxstyle='round', facecolor='wheat', alpha=0.8)
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', dpi=150)
plt.close()
print("图像已保存: figure.png")

View File

@@ -0,0 +1,134 @@
# 二次函数 $y = -2x^2 + 8x - 3$ 求解报告
## 1. 🎯 问题描述
已知二次函数 $y = -2x^2 + 8x - 3$,求:
1. 函数的顶点坐标
2. 函数的最大值
## 2. ✅ 最终结论
这是一个开口向下的抛物线(因为二次项系数 $a = -2 < 0$),通过配方可以将其化为顶点式 $y = -2(x-2)^2 + 5$。
**顶点坐标为 $(2, 5)$**,这意味着抛物线的对称轴是直线 $x = 2$。
由于抛物线开口向下,函数在顶点处取得**最大值 $y_{\max} = 5$**,此时 $x = 2$。
## 3. 📈 可视化
![函数图像](figure.png)
**图表说明**
- **蓝色曲线**:二次函数 $y = -2x^2 + 8x - 3$ 的图像
- **红色圆点**:顶点 $(2, 5)$,即函数取得最大值的位置
- **绿色虚线**:对称轴 $x = 2$
- **橙色点线**:最大值水平线 $y = 5$
- **绿色三角**:与 $x$ 轴的两个交点
- **紫色方块**:与 $y$ 轴的交点 $(0, -3)$
## 4. 🧠 数学建模与解题过程
<details>
<summary><strong>点击展开</strong></summary>
### 问题分析
这是一个标准的二次函数求顶点问题。二次函数的一般形式为 $y = ax^2 + bx + c$,其中:
- $a = -2$(决定开口方向和宽窄)
- $b = 8$
- $c = -3$
### 方法一:配方法
将二次函数化为顶点式 $y = a(x-h)^2 + k$
$$
\begin{aligned}
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
\end{aligned}
$$
从顶点式可直接读出:顶点坐标为 $(2, 5)$。
### 方法二:公式法
对于二次函数 $y = ax^2 + bx + c$,顶点坐标公式为:
$$
x_{\text{顶点}} = -\frac{b}{2a} = -\frac{8}{2 \times (-2)} = -\frac{8}{-4} = 2
$$
将 $x = 2$ 代入原函数:
$$
y_{\text{顶点}} = -2 \times 2^2 + 8 \times 2 - 3 = -8 + 16 - 3 = 5
$$
### 方法三:求导法(微积分验证)
对函数求导:
$$
y' = \frac{d}{dx}(-2x^2 + 8x - 3) = -4x + 8
$$
令 $y' = 0$,解得 $x = 2$。
二阶导数 $y'' = -4 < 0$,确认 $x = 2$ 处为极大值点。
### 最大值判断
由于 $a = -2 < 0$,抛物线开口向下,函数在顶点处取得最大值:
$$
y_{\max} = 5 \quad (\text{当 } x = 2 \text{ 时})
$$
</details>
## 5. 📊 运行结果
<details>
<summary><strong>点击展开</strong></summary>
```
==================================================
二次函数 y = -2x² + 8x - 3 求解
==================================================
1. 顶点坐标
使用公式 x = -b/(2a) = -8/(2×-2) = 2.0
代入求 y = -2×2.0² + 8×2.0 + (-3) = 5.0
顶点坐标: (2, 5)
2. 函数最大值
由于 a = -2 < 0抛物线开口向下
函数在顶点处取得最大值
最大值: y_max = 5
==================================================
SymPy 符号计算验证
==================================================
导数: y' = 8 - 4*x
令 y' = 0解得 x = [2]
二阶导数: y'' = -4 < 0确认为最大值点
顶点坐标: (2, 5)
最大值: 5
==================================================
配方形式验证
==================================================
配方形式: y = -2(x - 2)² + 5
展开验证: -2*x**2 + 8*x - 3
与原式相等: True
```
</details>

View File

@@ -0,0 +1,71 @@
# /// 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 = -2(x² - 4x) - 3
# y = -2(x² - 4x + 4 - 4) - 3
# y = -2(x - 2)² + 8 - 3
# y = -2(x - 2)² + 5
# 方法2使用公式 x_vertex = -b/(2a)
a = -2
b = 8
c = -3
x_vertex = -b / (2*a)
y_vertex = a * x_vertex**2 + b * x_vertex + c
print(f"\n1. 顶点坐标")
print(f" 使用公式 x = -b/(2a) = -{b}/(2×{a}) = {x_vertex}")
print(f" 代入求 y = {a}×{x_vertex}² + {b}×{x_vertex} + ({c}) = {y_vertex}")
print(f" 顶点坐标: ({int(x_vertex)}, {int(y_vertex)})")
print(f"\n2. 函数最大值")
print(f" 由于 a = {a} < 0抛物线开口向下")
print(f" 函数在顶点处取得最大值")
print(f" 最大值: y_max = {int(y_vertex)}")
# 使用 SymPy 验证
print("\n" + "=" * 50)
print("SymPy 符号计算验证")
print("=" * 50)
# 求导数找极值点
dy = sp.diff(y, x)
critical_points = sp.solve(dy, x)
print(f"\n导数: y' = {dy}")
print(f"令 y' = 0解得 x = {critical_points}")
# 验证是最大值(二阶导数 < 0
d2y = sp.diff(dy, x)
print(f"二阶导数: y'' = {d2y} < 0确认为最大值点")
# 计算顶点处的函数值
x_v = critical_points[0]
y_v = y.subs(x, x_v)
print(f"\n顶点坐标: ({x_v}, {y_v})")
print(f"最大值: {y_v}")
# 配方形式
print("\n" + "=" * 50)
print("配方形式验证")
print("=" * 50)
vertex_form = -2*(x - 2)**2 + 5
expanded = sp.expand(vertex_form)
print(f"配方形式: y = -2(x - 2)² + 5")
print(f"展开验证: {expanded}")
print(f"与原式相等: {sp.simplify(expanded - y) == 0}")