《用 Python 学微积分》原文见参考资料 1。

13、大 O 记法

比较两个函数时,我们会想知道,随着输入值 x 的增长或减小,两个函数的输出值增长或减小的速度究竟谁快谁慢。通过绘制函数图,我们可以获得一些客观的感受。

比较 x!、ex、x3 和 log(x) 的变化趋势。

import numpy as np
import sympy
import matplotlib.pyplot as plt

x = range(1,7)
factorial = [np.math.factorial(i) for i in x]
exponential = [np.e**i for i in x]
polynomial = [i**3 for i in x]
logarithmic = [np.log(i) for i in x]

plt.plot(x,factorial,'black',\
         x,exponential, 'blue',\
         x,polynomial, 'green',\
          x,logarithmic, 'red')

plt.show()
View Code

相关文章:

  • 2021-11-10
  • 2021-07-24
  • 2021-06-01
  • 2021-09-11
  • 2021-07-01
  • 2021-08-20
  • 2021-08-20
猜你喜欢
  • 2021-10-21
  • 2022-12-23
  • 2021-04-26
  • 2021-06-09
  • 2021-12-17
  • 2021-11-26
  • 2022-02-09
相关资源
相似解决方案