You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
524 B
25 lines
524 B
def central_difference(f, x, h):
|
|
"""
|
|
中心差分法进行数值微分(计算导数)
|
|
|
|
参数:
|
|
- f: 要求导数的函数
|
|
- x: 求导点的横坐标
|
|
- h: 步长
|
|
|
|
返回:
|
|
- 导数的近似值
|
|
"""
|
|
df = (f(x + h) - f(x - h)) / (2 * h) # 中心差分法计算导数
|
|
|
|
return df
|
|
|
|
def f(x):
|
|
return x**2
|
|
|
|
if __name__ == '__main__':
|
|
x = 2 # 求导点的横坐标
|
|
h = 0.01 # 步长
|
|
|
|
df = central_difference(f, x, h)
|
|
print("导数的近似值:", df) |