Manim实现:数列极限的定义可视化

1.函数的极限

数列 {x_{n}} 的极限为 L 当且仅当对于任意 ϵ>0,存在 N∈N,使得当 n>N 时,∣x_{n}−L∣<ϵ

\text{数列 } \{x_n\} \text{ 的极限为 } L \text{ 当且仅当对于任意 }
 \epsilon > 0, \text{ 存在 } N \in \mathbb{N}, \text{ 使得当 } n > N \text{ 时,}
|x_n - L| < \epsilon.

 

要在 Manim 中实现极限的证明,我们可以使用 Matplotlib 来生成必要的图像,展示数列 xnxn​ 收敛到 AA。我们将可视化数列行为,并用对给定极限的定义进行说明。

下面是一个简单的 Manim 代码示例,展示了数列x_{n} 趋近于 A 的过程,并可视化了这个极限定义的关键部分。

Manim 代码示例

from manim import *  

class LimitProof(Scene):  
    def construct(self):  
        # Create axes  
        axes = Axes(  
            x_range=[0, 10, 1],  
            y_range=[1, 5, 1],  
            axis_config={"color": BLUE},  
        )  

        # Add the axis to the scene  
        self.play(Create(axes))  
        self.wait(1)  

        # Define the limit A  
        A = 3  
        limit_line = axes.get_horizontal_line(A, color=YELLOW)  
        self.play(Create(limit_line))  

        # Number line for limit A  
        A_label = axes.get_y_axis_label('A', label_scale=0.7, buff=0.1)  
        self.play(Write(A_label))  

        # Define epsilon neighborhood  
        epsilon = 0.5  
        neighborhood_upper = A + epsilon  
        neighborhood_lower = A - epsilon  
        epsilon_band = axes.get_horizontal_line(neighborhood_upper, color=RED, opacity=0.5)  
        epsilon_band_lower = axes.get_horizontal_line(neighborhood_lower, color=RED, opacity=0.5)  

        self.play(Create(epsilon_band), Create(epsilon_band_lower))  

        # Graph x_n approaching A  
        x_values = range(1, 11)  
        y_values = [A - 0.1 * (1/n) if n < 10 else A for n in x_values]  # Approaching A as n increases  
        dot_list = [Dot(axes.c2p(n, y), color=GREEN) for n, y in zip(x_values, y_values)]  
        
        # Display points  
        self.play(*[Create(dot) for dot in dot_list])  
        self.wait(2)  

        # Define the epsilon definition of limit  
        definition = MathTex(  
            r"\forall \varepsilon > 0, \exists N \in \mathbb{N}, \forall n > N, |x_n - A| < \varepsilon"  
        ).scale(0.5).to_edge(DOWN)  

        self.play(Write(definition))  
        self.wait(3)  

        # Highlight N point and related points  
        N_value_indicator = axes.get_vertical_line(7, color=WHITE, dashed=True)  
        self.play(Create(N_value_indicator))  
        N_label = axes.get_x_axis_label('N', label_scale=0.5, buff=0.1)  
        self.play(Write(N_label))  
        self.wait(2)  

        # Show closing out the epsilon neighborhood  
        self.play(Uncreate(epsilon_band), Uncreate(epsilon_band_lower))  
        self.play(Create(limit_line))  
        self.wait(2)  

        # Final cleanup  
        self.clear()

 

说明

  1. Axes: 创建坐标轴,设置范围。
  2. Limit Line: 绘制水平线表示极限 A。
  3. Epsilon Neighborhood: 使用红色带表示 ϵ附近的区域。
  4. 数列: 使用绿色点表示数列 xnxn​ 随着 nn 的增加如何逐渐收敛到 A。
  5. 极限定义: 使用 MathTex 在底部显示极限定义。
  6. 可视化: 显示一个指示器在 N的位置,强调对于每个 ϵ 存在这样的 N。

 改正后的代码如下:

from manim import * 
import math

class LimitProof123(Scene):  
    def construct(self):  
        # 创建坐标轴  
        axes = Axes(  
            x_range=[0, 10, 1],  
            y_range=[-0.1, 1.1, 0.1],  
            axis_config={"color": BLUE},  
        )  
        print("Yes1")  
        
        self.play(Create(axes))  
        self.wait(1)  

        # 定义极限 L  
        L = 0   
        limit_line = Line(axes.c2p(0, L), axes.c2p(10, L), color=YELLOW, stroke_width=2)  
        self.play(Create(limit_line))    
        print("Yes2")  

        # 定义 epsilon 邻域  
        epsilon = 0.05  
        print("Yes3")
        
        epsilon_band_upper = Line(axes.c2p(0, L + epsilon),
                                  axes.c2p(10, L + epsilon), color=RED, stroke_width=1)  
        epsilon_band_lower = Line(axes.c2p(0, L - epsilon), 
                                  axes.c2p(10, L - epsilon), color=RED, stroke_width=1)  
        print("Yes4")
        self.play(Create(epsilon_band_upper), Create(epsilon_band_lower))  

        # 数列项的生成  
        x_values = [n for n in range(1, 21)]  # n 从 1 到 20  
        print("x_values:", x_values)  # 检查 x_values  
        y_values = [1/n for n in x_values]  # 计算 x_n = 1/n  
        print("y_values:", y_values)  # 检查 y_values 
        
        print(2)

        # 创建点  
        dots = []  
        for n, y in zip(x_values, y_values):  
            point = axes.c2p(n, y)  # 将 n 和 y 转换为坐标  
            print(f"n={n}, y={y}, is in epsilon band: {L - epsilon < y < L + epsilon}")  # 检查每个点是否在邻域内  
            dot = Dot(point, color=GREEN)  # 创建点  
            dots.append(dot)  # 添加到列表中   
        print("Yes01")
        # 使用单独的播放指令显示每个点  
        self.play(*[Create(dot) for dot in dots])  # 循环创建所有的点  
        print("Yes02")
        # 显示定义  
        definition = MathTex(  
            r"\forall \varepsilon > 0, \exists N \in \mathbb{N}, \forall n > N, |x_n - L| < \varepsilon"  
        ).scale(0.5).to_edge(DOWN)  
        print("Yes03")
        self.play(Write(definition))  
        self.wait(3)  

        # 选择 N,并高亮 N 点 
        print("Yes42")
        
        # 然后在代码中  
        N_value = math.ceil(1 / epsilon)
        print("N_value:", N_value)
        
        # 创建并显示 N 的虚线  
        N_value_indicator = DashedLine(start=axes.c2p(N_value, 0), end=axes.c2p(N_value, 1), color=WHITE)  
          
        self.play(Create(N_value_indicator))  
        self.wait(2)  # 等待以查看结果  
        
        print("Yes04")
        
        N_label = axes.get_x_axis_label(r"N = \lceil \frac{1}{\varepsilon} \rceil", buff=0.1)  
        self.play(Write(N_label))  
        self.wait(2)  

        # 清除 epsilon 邻域  
        self.play(Uncreate(epsilon_band_upper), Uncreate(epsilon_band_lower))  
        self.play(Create(limit_line))  
        self.wait(2)
        

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yasen.M

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值