20260109_170000_quadratic_vertex工具iflow模型qwen3-coder-plus
This commit is contained in:
@@ -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