李沐动手学深度学习4——微积分
李沐动手学深度学习,有视频、有中文电子书(含配套代码),推荐!
画图直接用matplotlib库实现,没什么知识点,代码如下:
import matplotlib.pyplot as plt import numpy as np def f(x): return 3 * x**2 - 4 * x def lim(f, x, h): return(f(x+h) - f(x)) / h h = 0.1 for i in range(5): print(f'h={h:.5f}, limit={lim(f, 1, h):.5f}') h *= 0.1 fig, ax = plt.subplots(figsize=(12, 8)) x = np.linspace(0, 3, 20) ax.plot(x, f(x), label='f(x)', color='blue', linestyle='-') ax.plot(x, 2*x-3, label='Tangent line (x=1)', color='purple', linestyle='--') ax.legend(fontsize=12) ax.grid(True, linestyle=':') ax.set_xlabel("x", fontsize=15) ax.set_ylabel("f(x)", fontsize=15) plt.show()