背景
你好-我是一位新的数学老师,我在思考如何解释导数的概念。我想做一个gif或视频(不关心哪一条)的切线沿一张图移动。
我尝试过许多不同的方法,我读过的大多数视频/帖子都显示了一个以x变化构造的图形.这并不是我想要的,因为下面这些视频,我也可以构造我的图形。但是我想要一条直线,在图的每一点上改变它的斜率。
# Define parabola
def f(x):
return 5*x**3-2*x**2-2*x
# Define parabola derivative
def slope(x):
return 15*x**2-4*x-2
# Define tangent line
def line(x, x1, y1):
return slope(x1)*(x - x1) + y1我的问题
有什么好的例子来说明如何做到这一点吗?也许是视频或者代码,我可以从中寻找灵感?
我知道我的问题有点含糊不清,但事先谢谢
发布于 2022-06-22 14:47:33
您可以使用matplotlib提供的FuncAnimation函数(参见doc 这里)。
下面是您提供的代码的一个示例:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
# Define parabola
def f(x):
return 5*x**3-2*x**2-2*x
# Define parabola derivative
def slope(x):
return 15*x**2-4*x-2
# Define tangent line
def line(x, x1, y1):
return slope(x1)*(x - x1) + y1
###Set up animation###
fig, ax = plt.subplots()
range_x=np.linspace(-1,1,100,endpoint=True)
range_slope=np.linspace(-1,1,100,endpoint=True)
plt.plot(range_x, f(range_x),'ro',label='Parabola')
ln_slope,=plt.plot([],[],'tab:blue',label='Tangent',lw=3.5)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend(loc='upper right')
def init():
ax.set_xlim(-1,1)
ax.set_ylim(-6, 3)
return ln_slope,
def update(frame):
ydata=line(range_slope,range_x[frame],f(range_x[frame]))
ln_slope.set_data(range_slope, ydata)
return ln_slope,
ani = FuncAnimation(fig, update, frames=100,init_func=init, blit=True)
plt.show()输出结果是:

https://stackoverflow.com/questions/72716691
复制相似问题