Files
ai-math-test/01-基石题-test/模型Opus45工具ClaudeCode/solve.py
2026-01-09 17:17:01 +08:00

62 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# /// 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【方法2SymPy 求导验证】")
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)} 处取得)")