首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >切线在图上的动画运动

切线在图上的动画运动
EN

Stack Overflow用户
提问于 2022-06-22 13:45:49
回答 1查看 64关注 0票数 0

背景

你好-我是一位新的数学老师,我在思考如何解释导数的概念。我想做一个gif或视频(不关心哪一条)的切线沿一张图移动。

我尝试过许多不同的方法,我读过的大多数视频/帖子都显示了一个以x变化构造的图形.这并不是我想要的,因为下面这些视频,我也可以构造我的图形。但是我想要一条直线,在图的每一点上改变它的斜率。

代码语言:javascript
复制
# 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

我的问题

有什么好的例子来说明如何做到这一点吗?也许是视频或者代码,我可以从中寻找灵感?

我知道我的问题有点含糊不清,但事先谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-22 14:47:33

您可以使用matplotlib提供的FuncAnimation函数(参见doc 这里)。

下面是您提供的代码的一个示例:

代码语言:javascript
复制
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()

输出结果是:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72716691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档