ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

函数极限可视化代码分析与详解

函数极限可视化代码分析与详解 from manim import * import numpy as np import math class LimitVisualization(Scene): def construct(self): #背景颜色就是黑板颜色一样黑绿颜色 self.camera.background_color#003311 # 标题 title Text(函数极限的可视化, font_size48, colorBLUE) title.to_edge(UP) self.play(Write(title)) self.wait(1) # 例子1: lim(x→0) sin(x)/x 1 self.example1() self.clear() self.wait(0.5) # 例子2: lim(x→∞) (1 1/x)^x e #使用VGroup组合 title2 VGroup(Text(例2:, font_size36, colorYELLOW), MathTex(r\lim_{x \to \infty} \left(1\frac{1}{x}\right)^x e \approx 2.71828, font_size36, colorYELLOW)).arrange(RIGHT, buff0.3).to_edge(UP) #self.play(Write(title2)) self.example2() self.clear() self.wait(0.5) # 例子3: lim(x→2) (x²-4)/(x-2) 4 title3 VGroup(Text(例3:, font_size36, colorYELLOW), MathTex(r\lim_{x \to 2} \frac{x^{2}-4}{x-2} 4, font_size36, colorYELLOW)).arrange(RIGHT, buff0.3).to_edge(UP) #title3 Text(例3: lim(x→2) (x²-4)/(x-2) 4, font_size36, colorGREEN) title3.to_edge(UP) self.play(Write(title3)) self.example3() self.clear() self.wait(0.5) # 例子4: lim(x→∞) sin(x)/x 0 title4 VGroup(Text(例4:, font_size36, colorYELLOW), MathTex(r\lim_{x \to \infty} \frac{sin(x)}{x} 0, font_size36, colorYELLOW)).arrange(RIGHT, buff0.3).to_edge(UP) #title4 Text(例4: lim(x→∞) sin(x)/x 0, font_size36, colorORANGE) title4.to_edge(UP) self.play(Write(title4)) self.example4() self.wait(2) def example1(self): # 例1: lim(x→0) sin(x)/x 1 example_text VGroup( Text(例1:, font_size36, colorYELLOW), MathTex(r\lim_{x \to 0} \frac{sin(x)}{x}1 , font_size36, colorYELLOW)).arrange(RIGHT, buff0.3).shift(2*UP) self.play(Write(example_text)) # 创建坐标轴 axes Axes( x_range[-6, 6, 1], y_range[-0.5, 1.5, 0.5], x_length8, y_length4, axis_config{color: BLUE} ) axes_labels axes.get_axis_labels(x_labelx, y_labely) # 创建函数图形 graph axes.plot( lambda x: np.sin(x)/x if x ! 0 else 1, x_range[-6, -0.01], colorYELLOW ) graph2 axes.plot( lambda x: np.sin(x)/x if x ! 0 else 1, x_range[0.01,6], colorYELLOW ) graph2.points graph2.points[::-1] # 或者使用 graph2.reverse_points() # 标记极限点 point Dot(axes.coords_to_point(0, 1), colorRED) point_label MathTex((0, 1), colorRED).next_to(point, RIGHT) li1 VGroup(axes, axes_labels, graph, graph2, point, point_label).shift(1.5*DOWN) self.play(Create(axes), Write(axes_labels)) self.play(Create(graph),Create(graph2)) # 右侧反向创建从右到左 self.play(Create(point), Write(point_label)) self.play(li1.animate.scale(0.5).move_to(LEFT * 5 )) # 显示极限计算 limit_text MathTex(r\lim_{x \to 0} \frac{\sin x}{x} 1, font_size36, colorGREEN).shift(RIGHT) self.play(Write(limit_text)) # 显示数值计算 values_text Text(数值验证:, font_size36, colorWHITE) values_text.next_to(limit_text, DOWN) self.play(Write(values_text)) values [ x 0.1: sin(0.1)/0.1 0.9983, x 0.01: sin(0.01)/0.01 0.99998, x 0.001: sin(0.001)/0.001 0.9999998 ] for i, v in enumerate(values): text Text(v, font_size25, colorBLUE_C) text.next_to(values_text, DOWN, buff0.5 i * 0.4) # 每次递增 self.play(Write(text)) self.wait(2) def example2(self): # 例2: lim(x→∞) (11/x)^x e example_text VGroup(Text(例2:, font_size36, colorYELLOW), MathTex(r\lim_{x \to \infty} \left(1\frac{1}{x}\right)^x e \approx 2.71828, font_size36, colorYELLOW)).arrange(RIGHT, buff0.3).shift(2*UP) self.play(Write(example_text)) # 创建坐标轴 axes Axes( x_range[0, 100, 10], y_range[0, 4, 1], x_length8, y_length4, axis_config{color: BLUE} ) axes_labels axes.get_axis_labels(x_labelx, y_labely) # 创建函数图形 graph axes.plot( lambda x: (1 1/x)**x, x_range[0.01, 100], colorORANGE ) self.play(Create(axes), Write(axes_labels)) self.play(Create(graph)) # 添加水平渐近线 ye e_line DashedLine( startaxes.coords_to_point(0, np.e), endaxes.coords_to_point(100, np.e), colorGREEN ) e_label MathTex(ry e \approx 2.718, colorGREEN).next_to(e_line, LEFT) self.play(Create(e_line), Write(e_label)) li1 VGroup(axes, axes_labels, graph,e_line,e_label) self.play(li1.animate.scale(0.5).rotate(-1.5).move_to(LEFT * 5 )) # 显示数值计算 values_text Text(数值验证:, font_size24, colorWHITE) self.play(Write(values_text)) values [ x 10: (11/10)^10 2.5937, x 100: (11/100)^100 2.7048, x 1000: (11/1000)^1000 2.7169 ] for i, v in enumerate(values): text Text(v, font_size20, colorBLUE_C) text.next_to(values_text, DOWN,buff0.5 i * 0.4) self.play(Write(text)) self.wait(2) def example3(self): # 例3: lim(x→2) (x²-4)/(x-2) 4 example_text MathTex(r\lim_{x \to 2} \frac{x^{2}-4}{x-2} 4, font_size36, colorGREEN).shift(2*UP) #example_text.to_edge(UP) self.play(Write(example_text)) # 创建坐标轴 axes Axes( x_range[-2, 6, 1], y_range[-2, 8, 2], x_length8, y_length4, axis_config{color: BLUE} ) axes_labels axes.get_axis_labels(x_labelx, y_labely) # 创建函数图形去除x2处的间断点 graph1 axes.plot( lambda x: (x**2 - 4)/(x - 2) if x ! 2 else 4, x_range[-2, 1.99], colorGREEN ) graph2 axes.plot( lambda x: (x**2 - 4)/(x - 2) if x ! 2 else 4, x_range[2.01, 6], colorGREEN ) graph2.points graph2.points[::-1] # 或者使用 graph2.reverse_points() self.play(Create(axes), Write(axes_labels)) self.play(Create(graph1), Create(graph2)) # 标记极限点 point Dot(axes.coords_to_point(2, 4), colorRED, radius0.15) point_label MathTex((2, 4), colorRED).next_to(point, UR) open_circle Circle(colorRED, radius0.15).move_to(axes.coords_to_point(2, 4)) open_circle.set_fill(RED, opacity1) #//////////////////// self.play(Create(point), Write(point_label)) #_______________________________ li1 VGroup(axes, axes_labels, graph1, graph2,point,point_label,open_circle) self.play(li1.animate.scale(0.5).move_to(LEFT * 5 )) # 显示函数分解 formula_text MathTex(r\frac{x^2-4}{x-2} \frac{(x-2)(x2)}{x-2} x2, font_size30, colorWHITE) formula_text.next_to(example_text, DOWN, buff0.5) self.play(Write(formula_text)) # 显示数值计算 values_text Text(数值验证:, font_size24, colorWHITE) values_text.next_to(formula_text, DOWN, buff0.5) self.play(Write(values_text)) values [ x 1.9: (1.9²-4)/(1.9-2) 3.9, x 1.99: (1.99²-4)/(1.99-2) 3.99, x 2.01: (2.01²-4)/(2.01-2) 4.01 ] for i, v in enumerate(values): text Text(v, font_size20, colorBLUE_C) text.next_to(values_text, DOWN,buff0.5 i * 0.4) self.play(Write(text)) self.wait(2) def example4(self): # 例4: lim(x→∞) sin(x)/x 0 example_text VGroup(Text( , font_size36, colorYELLOW), MathTex(r\lim_{x \to \infty} \frac{sin(x)}{x} 0, font_size36, colorYELLOW)).arrange(RIGHT, buff0.3).shift(2*UP) self.play(Write(example_text)) # 创建坐标轴 axes Axes( x_range[0, 50, 5], y_range[-1, 1, 0.5], x_length8, y_length4, axis_config{color: BLUE} ) axes_labels axes.get_axis_labels(x_labelx, y_labely) # 创建函数图形 graph axes.plot( lambda x: np.sin(x)/x if x ! 0 else 1, x_range[0.01, 50], colorORANGE ) # 创建包络线 env_upper axes.plot( lambda x: 1/x, x_range[0.5, 50], colorRED, stroke_width2 ) env_lower axes.plot( lambda x: -1/x, x_range[0.5, 50], colorRED, stroke_width2 ) # 添加水平渐近线 zero_line DashedLine( startaxes.coords_to_point(0, 0), endaxes.coords_to_point(50, 0), colorGREEN ) zero_label MathTex( 0, colorGREEN).next_to(zero_line, 0.5*LEFT) self.play(Create(axes), Write(axes_labels)) self.play( Write(zero_label)) self.play(Create(graph),Create(zero_line),Create(env_upper), Create(env_lower),run_time4) # 显示解释 explain_text VGroup(Text(sin(x)有界:, font_size24, colorYELLOW), MathTex(r\frac{1}{x}, font_size34, colorYELLOW), Text(趋于0因此极限为0:, font_size24, colorYELLOW) ).arrange(RIGHT, buff0.75).shift(UP) self.play(Write(explain_text)) # 显示数值计算 values_text Text(数值验证:, font_size24, colorWHITE).shift(DOWN) self.play(Write(values_text)) values [ x 10: sin(10)/10 -0.0544, x 50: sin(50)/50 -0.00214, x 100: sin(100)/100 -0.00506 ] for i, v in enumerate(values): text Text(v, font_size20, colorBLUE_C) text.next_to(values_text, DOWN,buff0.5 i * 0.4) self.play(Write(text)) self.wait(2)一、整体架构分析1.1 代码结构总览这段代码是一个基于Manim的数学教学动画程序通过可视化的方式展示了四个经典的函数极限问题。程序的结构可以总结如下LimitVisualization (场景类) │ ├── construct() - 主入口方法 │ ├── 设置背景色 │ ├── 添加主标题 │ ├── 依次调用四个示例方法 │ └── 管理场景切换 │ ├── example1() - 例1: lim(x→0) sin(x)/x 1 ├── example2() - 例2: lim(x→∞) (11/x)^x e ├── example3() - 例3: lim(x→2) (x²-4)/(x-2) 4 └── example4() - 例4: lim(x→∞) sin(x)/x 01.2 核心设计思想教学分层设计每个示例遵循“展示 → 视觉化 → 数值验证”的教学逻辑通过动画逐步引入数学概念使抽象的极限概念变得直观二、关键代码段分析2.1 场景初始化class LimitVisualization(Scene): def construct(self): # 设置深绿色背景模拟黑板效果 self.camera.background_color#003311 # 添加主标题 title Text(函数极限的可视化, font_size48, colorBLUE) title.to_edge(UP) self.play(Write(title))设计亮点选择深绿色背景(#003311)模拟黑板营造课堂氛围使用Write()动画让标题逐字出现增强视觉吸引力2.2 例1sin(x)/x 的极限def example1(self): # 创建坐标轴精确控制范围和大小 axes Axes( x_range[-6, 6, 1], # x轴范围-6到6间隔1 y_range[-0.5, 1.5, 0.5], # y轴范围 x_length8, # x轴长度像素 y_length4 # y轴长度 ) # 处理x0处的间断点分段绘制 graph axes.plot( lambda x: np.sin(x)/x if x ! 0 else 1, x_range[-6, -0.01], # 左半部分 colorYELLOW )关键技术处理间断点处理由于sin(x)/x在x0处未定义采用分段绘制的方式动画顺序先绘制坐标轴再绘制函数图像最后标记极限点# 右侧图像反向绘制实现从右到左的动画效果 graph2.points graph2.points[::-1]2.3 例2极限e的显示# 添加水平渐近线 ye e_line DashedLine( startaxes.coords_to_point(0, np.e), endaxes.coords_to_point(100, np.e), colorGREEN )教学特点渐近线可视化用虚线段清晰标示极限值e的位置对比展示将图形缩小后移到左侧腾出空间展示数值计算2.4 例3可去间断点# 标记极限点同时绘制实心点和空心圆 point Dot(axes.coords_to_point(2, 4), colorRED, radius0.15) open_circle Circle(colorRED, radius0.15) open_circle.set_fill(RED, opacity1)数学意义同时使用实心点和空心圆展示函数在该点的“补充定义”思想通过图形分解演示(x²-4)/(x-2) x2x≠2时2.5 例4夹逼定理的运用# 创建包络线展示 sin(x)/x 的界限 env_upper axes.plot(lambda x: 1/x, x_range[0.5, 50], colorRED) env_lower axes.plot(lambda x: -1/x, x_range[0.5, 50], colorRED)数学原理可视化用红色包络线(±1/x)直观展示夹逼定理清晰呈现sin(x)/x被两条曲线夹住并趋近于0的过程三、动画技术与数学教学的创新结合3.1 渐进式学习流程每个示例的动画流程都是步骤展示 → 图形绘制 → 极限标记 → 数值验证3.2 空间复用策略# 缩放并移动图形到左侧 li1.animate.scale(0.5).move_to(LEFT * 5)空间规划图形区与文字区并行同时利用屏幕空间缩放操作让用户既有整体图形预览又能看到具体数值3.3 色彩编码系统元素类型颜色用途坐标轴蓝色基础框架函数图像黄色/橙色/绿色区分不同函数极限点/线红/绿强调极限位置标题公式黄色核心数学内容数值验证蓝青色辅助信息四、代码优化建议4.1 代码复用改进def create_axes(self, x_range, y_range, x_labelx, y_labely): 统一的坐标轴创建函数 axes Axes( x_rangex_range, y_rangey_range, axis_config{color: BLUE} ) return VGroup(axes, axes.get_axis_labels(x_labelx_label, y_labely_label))4.2 动画控制改进# 使用并行动画提高效率 self.play( Create(graph), Create(env_upper), Create(env_lower), run_time4 )4.3 参数配置优化# 将常量提取为配置 CONFIG { background_color: #003311, axis_color: BLUE, title_size: 48, example_text_size: 36 }五、总结这个代码示例展示了如何将数学极限这一抽象概念通过动画可视化视觉教学设计通过颜色、动画和空间布局强化教学效果数学逻辑严谨正确处理特殊点间断点和极限概念多层信息呈现图形展示 计算过程 数值验证的多维度学习该代码不仅是一个数学可视化工具更是一个教学方法的创新实践让学习者能够直观地理解深奥的数学概念。
返回列表