我试图找到Python生成函数根的二分数。例如,如果我有代码:
import math
a=1
b=2
def f(x):
return x**3+x-7
while b-a>0.001:
c=(a+b)/2
if f(a)*f(c)>0:
a=c
else:
b=c
print(c)
此代码只会产生所需的答案。我想知道Python执行二分法的次数以及每次的值是多少。
答案 0 :(得分:2)
您可以使用某些分析器。例如 line_profiler 。你可以简单地安装它:
pip install line_profiler