20260109_160634_quadratic_vertex-ClaudeCode+Opus45
This commit is contained in:
61
20260109_160634_quadratic_vertex-ClaudeCode+Opus45/solve.py
Normal file
61
20260109_160634_quadratic_vertex-ClaudeCode+Opus45/solve.py
Normal file
@@ -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