Files
task-3-3-2-MLP/hh.py
2026-05-21 16:29:48 +08:00

111 lines
3.8 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.

import math
# ====================== 1.2 导数相关 ======================
print("="*50)
print("1.2 导数——变化的度量")
print("="*50)
# ---------------------- 1.2.1 导数定义(数值求导) ----------------------
def numerical_derivative(f, x, h=1e-5):
"""
根据导数定义实现数值求导:
f'(x) = lim(h→0) [f(x+h) - f(x)] / h
"""
return (f(x + h) - f(x)) / h
# 测试常见函数的导数和1.2.2对应)
print("\n--- 1.2.1 & 1.2.2 常见函数的导数测试 ---")
x = 2 # 测试点
# 1. 常数函数 f(x) = 5
def const_func(x):
return 5
print(f"常数函数 f(x)=5 在x={x}处的导数:{numerical_derivative(const_func, x):.4f} 理论值0")
# 2. 幂函数 f(x)=x²
def square_func(x):
return x**2
print(f"幂函数 f(x)=x² 在x={x}处的导数:{numerical_derivative(square_func, x):.4f} (理论值:{2*x}")
# 3. 幂函数 f(x)=x³
def cube_func(x):
return x**3
print(f"幂函数 f(x)=x³ 在x={x}处的导数:{numerical_derivative(cube_func, x):.4f} (理论值:{3*x**2}")
# 4. 指数函数 f(x)=e^x
def exp_func(x):
return math.exp(x)
print(f"指数函数 f(x)=e^x 在x={x}处的导数:{numerical_derivative(exp_func, x):.4f} (理论值:{math.exp(x):.4f}")
# 5. 对数函数 f(x)=ln(x)
def log_func(x):
return math.log(x)
print(f"对数函数 f(x)=ln(x) 在x={x}处的导数:{numerical_derivative(log_func, x):.4f} (理论值:{1/x}")
# 6. 三角函数 f(x)=sin(x)
def sin_func(x):
return math.sin(x)
print(f"三角函数 f(x)=sin(x) 在x={x}处的导数:{numerical_derivative(sin_func, x):.4f} (理论值:{math.cos(x):.4f}")
# 7. 三角函数 f(x)=cos(x)
def cos_func(x):
return math.cos(x)
print(f"三角函数 f(x)=cos(x) 在x={x}处的导数:{numerical_derivative(cos_func, x):.4f} (理论值:{-math.sin(x):.4f}")
# ---------------------- 1.2.4 链式法则示例 ----------------------
print("\n--- 1.2.4 链式法则示例 ---")
# 例子1y = (2x + 1)^3
def chain_example1(x):
return (2*x + 1)**3
def chain_example1_derivative(x):
# 手动用链式法则计算的导数dy/dx = 3*(2x+1)^2 * 2 = 6*(2x+1)^2
return 6 * (2*x + 1)**2
x1 = 1
print(f"例子1y=(2x+1)^3 在x={x1}处的导数")
print(f"数值求导结果:{numerical_derivative(chain_example1, x1):.4f}")
print(f"链式法则手动计算结果:{chain_example1_derivative(x1):.4f}")
# 例子2y = e^(3x)
def chain_example2(x):
return math.exp(3*x)
def chain_example2_derivative(x):
# 手动用链式法则计算的导数dy/dx = e^(3x) * 3 = 3*e^(3x)
return 3 * math.exp(3*x)
x2 = 0.5
print(f"\n例子2y=e^(3x) 在x={x2}处的导数")
print(f"数值求导结果:{numerical_derivative(chain_example2, x2):.4f}")
print(f"链式法则手动计算结果:{chain_example2_derivative(x2):.4f}")
# ---------------------- 1.2.5 偏导数示例 ----------------------
print("\n--- 1.2.5 偏导数示例 ---")
def func_z(x, y):
# z = f(x,y) = x² + 3xy + y²
return x**2 + 3*x*y + y**2
def partial_derivative_x(x, y, h=1e-5):
# 对x的偏导数把y看作常数
return (func_z(x + h, y) - func_z(x, y)) / h
def partial_derivative_y(x, y, h=1e-5):
# 对y的偏导数把x看作常数
return (func_z(x, y + h) - func_z(x, y)) / h
# 测试点 (1, 2)
x_p, y_p = 1, 2
print(f"函数 z = x² + 3xy + y² 在点({x_p}, {y_p})处的偏导数:")
print(f"对x的偏导数值{partial_derivative_x(x_p, y_p):.4f} (理论值:{2*x_p + 3*y_p}")
print(f"对y的偏导数值{partial_derivative_y(x_p, y_p):.4f} (理论值:{3*x_p + 2*y_p}")
print("\n" + "="*50)
print("所有导数相关示例运行完毕!")
print("="*50)