20260109_170000_quadratic_vertex工具iflow模型qwen3-coder-plus
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 58 KiB |
@@ -0,0 +1,58 @@
|
|||||||
|
# /// script
|
||||||
|
# requires-python = ">=3.11"
|
||||||
|
# dependencies = ["numpy", "matplotlib"]
|
||||||
|
# ///
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
# 设置字体(优先使用中文字体,DejaVu Sans 作为 fallback)
|
||||||
|
plt.rcParams['font.sans-serif'] = ['WenQuanYi Micro Hei', 'Noto Sans CJK SC', 'Microsoft YaHei', 'SimHei', 'SimSun', 'DejaVu Sans']
|
||||||
|
plt.rcParams['axes.unicode_minus'] = False
|
||||||
|
|
||||||
|
# 绑定图像尺寸和 DPI
|
||||||
|
plt.figure(figsize=(8, 6), dpi=150)
|
||||||
|
|
||||||
|
# 定义二次函数 y = -2x^2 + 8x - 3
|
||||||
|
def quadratic_func(x):
|
||||||
|
return -2*x**2 + 8*x - 3
|
||||||
|
|
||||||
|
# 生成x值,覆盖关键区域
|
||||||
|
x = np.linspace(-1, 5, 400)
|
||||||
|
y = quadratic_func(x)
|
||||||
|
|
||||||
|
# 绘制函数曲线
|
||||||
|
plt.plot(x, y, label=r'$y = -2x^2 + 8x - 3$', color='blue', linewidth=2)
|
||||||
|
|
||||||
|
# 标出顶点 (2, 5)
|
||||||
|
vertex_x, vertex_y = 2, 5
|
||||||
|
plt.plot(vertex_x, vertex_y, 'ro', markersize=8, label=f'顶点 ({vertex_x}, {vertex_y})')
|
||||||
|
|
||||||
|
# 添加网格
|
||||||
|
plt.grid(True, linestyle='--', alpha=0.6)
|
||||||
|
|
||||||
|
# 设置坐标轴标签和标题
|
||||||
|
plt.xlabel('x', fontsize=12)
|
||||||
|
plt.ylabel('y', fontsize=12)
|
||||||
|
plt.title('二次函数 $y = -2x^2 + 8x - 3$ 的图像', fontsize=14)
|
||||||
|
|
||||||
|
# 设置坐标轴范围
|
||||||
|
plt.xlim(-1, 5)
|
||||||
|
plt.ylim(-5, 7)
|
||||||
|
|
||||||
|
# 添加图例
|
||||||
|
plt.legend()
|
||||||
|
|
||||||
|
# 在顶点处添加注释
|
||||||
|
plt.annotate(f'顶点\n({vertex_x}, {vertex_y})',
|
||||||
|
xy=(vertex_x, vertex_y),
|
||||||
|
xytext=(vertex_x + 1.2, vertex_y - 1),
|
||||||
|
arrowprops=dict(arrowstyle='->', color='red', lw=1.5),
|
||||||
|
fontsize=12,
|
||||||
|
color='red',
|
||||||
|
ha='center')
|
||||||
|
|
||||||
|
# 保存图像
|
||||||
|
plt.savefig('figure.png', bbox_inches='tight')
|
||||||
|
plt.close()
|
||||||
|
print("图像已保存: figure.png")
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
# 二次函数 $y = -2x^2 + 8x - 3$ 求解报告
|
||||||
|
|
||||||
|
## 1. 🎯 问题描述
|
||||||
|
|
||||||
|
已知二次函数 $y = -2x^2 + 8x - 3$,求:
|
||||||
|
1. 函数的顶点坐标
|
||||||
|
2. 函数的最大值
|
||||||
|
需要绘图。
|
||||||
|
|
||||||
|
## 2. ✅ 最终结论
|
||||||
|
|
||||||
|
对于二次函数 $y = -2x^2 + 8x - 3$:
|
||||||
|
1. 顶点坐标为 (2, 5)
|
||||||
|
2. 由于函数开口向下($a = -2 < 0$),顶点是函数的最大值点,因此函数的最大值为 5
|
||||||
|
|
||||||
|
## 3. 📈 可视化
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**图表说明**:
|
||||||
|
|
||||||
|
- 蓝色曲线:函数 $y = -2x^2 + 8x - 3$
|
||||||
|
- 红色圆点:顶点 (2, 5)
|
||||||
|
- 从图中可以清楚地看到函数开口向下,顶点为最高点
|
||||||
|
|
||||||
|
## 4. 🧠 数学建模与解题过程
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><strong>点击展开</strong></summary>
|
||||||
|
|
||||||
|
**问题分析**:这是一个标准的二次函数求顶点和最值问题。给定函数 $y = -2x^2 + 8x - 3$ 是一个开口向下的抛物线(因为二次项系数 $a = -2 < 0$),所以存在最大值。
|
||||||
|
|
||||||
|
**方法选择**:可以使用导数方法或顶点公式方法求解。
|
||||||
|
|
||||||
|
**推导过程**:
|
||||||
|
|
||||||
|
1. **使用导数方法**:
|
||||||
|
- 对函数 $y = -2x^2 + 8x - 3$ 求导:$y' = -4x + 8$
|
||||||
|
- 令导数为0:$-4x + 8 = 0$
|
||||||
|
- 解得 $x = 2$
|
||||||
|
|
||||||
|
2. **求顶点的y坐标**:
|
||||||
|
- 将 $x = 2$ 代入原函数:$y = -2(2)^2 + 8(2) - 3 = -8 + 16 - 3 = 5$
|
||||||
|
- 所以顶点坐标为 (2, 5)
|
||||||
|
|
||||||
|
3. **验证函数开口方向**:
|
||||||
|
- 二次项系数 $a = -2 < 0$,所以函数开口向下
|
||||||
|
- 因此顶点是函数的最大值点
|
||||||
|
|
||||||
|
4. **使用顶点公式验证**:
|
||||||
|
- 对于二次函数 $y = ax^2 + bx + c$,顶点坐标为 $(-\frac{b}{2a}, f(-\frac{b}{2a}))$
|
||||||
|
- 其中 $a = -2$, $b = 8$, $c = -3$
|
||||||
|
- $x = -\frac{8}{2 \times (-2)} = -\frac{8}{-4} = 2$
|
||||||
|
- $y = -2(2)^2 + 8(2) - 3 = 5$
|
||||||
|
- 验证结果一致
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
## 5. 📊 运行结果
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><strong>点击展开</strong></summary>
|
||||||
|
|
||||||
|
```
|
||||||
|
给定二次函数: y = -2*x**2 + 8*x - 3
|
||||||
|
函数的一阶导数: 8 - 4*x
|
||||||
|
导数为0的点: [2]
|
||||||
|
顶点坐标: (2, 5)
|
||||||
|
函数的二阶导数: -4
|
||||||
|
由于二次项系数为负数,函数开口向下,顶点为最大值点
|
||||||
|
函数的最大值: 5
|
||||||
|
|
||||||
|
使用顶点公式验证:
|
||||||
|
顶点x坐标: x = -b/(2a) = -8/(2*-2) = 2.0
|
||||||
|
顶点y坐标: y = -2*(2.0)^2 + 8*(2.0) + -3 = 5.0
|
||||||
|
|
||||||
|
========================================
|
||||||
|
最终答案:
|
||||||
|
1. 函数的顶点坐标: (2, 5)
|
||||||
|
2. 函数的最大值: 5
|
||||||
|
========================================
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
# /// script
|
||||||
|
# requires-python = ">=3.11"
|
||||||
|
# dependencies = ["sympy", "numpy", "matplotlib"]
|
||||||
|
# ///
|
||||||
|
|
||||||
|
import sympy as sp
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# 定义变量
|
||||||
|
x = sp.symbols('x', real=True)
|
||||||
|
|
||||||
|
# 定义二次函数 y = -2x^2 + 8x - 3
|
||||||
|
func = -2*x**2 + 8*x - 3
|
||||||
|
|
||||||
|
print("给定二次函数: y =", func)
|
||||||
|
|
||||||
|
# 方法1: 使用导数求顶点
|
||||||
|
# 顶点的x坐标是导数为0的点
|
||||||
|
derivative = sp.diff(func, x)
|
||||||
|
print("函数的一阶导数:", derivative)
|
||||||
|
|
||||||
|
# 求导数为0的点
|
||||||
|
critical_points = sp.solve(derivative, x)
|
||||||
|
print("导数为0的点:", critical_points)
|
||||||
|
|
||||||
|
# 顶点的x坐标
|
||||||
|
vertex_x = critical_points[0]
|
||||||
|
vertex_y = func.subs(x, vertex_x)
|
||||||
|
|
||||||
|
print(f"顶点坐标: ({vertex_x}, {vertex_y})")
|
||||||
|
|
||||||
|
# 判断最大值还是最小值
|
||||||
|
second_derivative = sp.diff(derivative, x)
|
||||||
|
print("函数的二阶导数:", second_derivative)
|
||||||
|
|
||||||
|
if second_derivative < 0:
|
||||||
|
print("由于二次项系数为负数,函数开口向下,顶点为最大值点")
|
||||||
|
print(f"函数的最大值: {vertex_y}")
|
||||||
|
elif second_derivative > 0:
|
||||||
|
print("由于二次项系数为正数,函数开口向上,顶点为最小值点")
|
||||||
|
print(f"函数的最小值: {vertex_y}")
|
||||||
|
else:
|
||||||
|
print("这不是二次函数")
|
||||||
|
|
||||||
|
# 方法2: 使用二次函数顶点公式验证
|
||||||
|
# 对于二次函数 y = ax^2 + bx + c,顶点x坐标为 x = -b/(2a)
|
||||||
|
a = -2
|
||||||
|
b = 8
|
||||||
|
c = -3
|
||||||
|
|
||||||
|
vertex_x_formula = -b / (2*a)
|
||||||
|
vertex_y_formula = a * vertex_x_formula**2 + b * vertex_x_formula + c
|
||||||
|
|
||||||
|
print("\n使用顶点公式验证:")
|
||||||
|
print(f"顶点x坐标: x = -b/(2a) = {-b}/(2*{a}) = {vertex_x_formula}")
|
||||||
|
print(f"顶点y坐标: y = {a}*({vertex_x_formula})^2 + {b}*({vertex_x_formula}) + {c} = {vertex_y_formula}")
|
||||||
|
|
||||||
|
# 最终结果
|
||||||
|
print("\n" + "="*40)
|
||||||
|
print("最终答案:")
|
||||||
|
print(f"1. 函数的顶点坐标: ({vertex_x}, {vertex_y})")
|
||||||
|
print(f"2. 函数的最大值: {vertex_y}")
|
||||||
|
print("="*40)
|
||||||
Reference in New Issue
Block a user