Manim中三种函数图像类的比较

Manim 库中,FunctionGraphImplicitFunctionParametricFunction 都是用于绘制函数图像的类,但它们的适用场景、输入形式和实现方式有显著区别。

以下是详细对比:

1. FunctionGraph

  • 用途 :绘制 显式函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> y = f ( x ) y = f(x) </math>y=f(x)) 的图像(单值函数)。
  • 输入要求
    接受一个 一元函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( x ) f(x) </math>f(x)) 和 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x) 的范围(如x_range=[-2, 2])。
  • 特点
    • 直接映射 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> x → ( x , f ( x ) ) x \to (x, f(x)) </math>x→(x,f(x)))。
    • 要求函数是 单值 的(一个 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x对应唯一 <math xmlns="http://www.w3.org/1998/Math/MathML"> y y </math>y)。
  • 示例代码
python 复制代码
class Example(Scene):
    def construct(self):
        # 绘制 y = x^2
        graph = FunctionGraph(lambda x: x**2, x_range=[-2, 2], color=BLUE)
        self.add(graph)

2. ImplicitFunction

  • 用途 :绘制 隐函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0) 的图像(如圆、椭圆)。
  • 输入要求
    接受一个 二元函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) F(x, y) </math>F(x,y)) 和 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> x , y x, y </math>x,y) 的范围(如x_range=[-3, 3], y_range=[-3, 3])。
  • 特点
    • 通过数值方法(如 Marching Squares 算法)求解满足 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0) 的点集。
    • 可绘制 多值曲线 (如一个 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x对应多个$y ))。
  • 示例代码
python 复制代码
class ImplicitFunctionExample(Scene):
    def construct(self):
        # 笛卡尔叶形线隐函数方程: x^3 + y^3 - 3axy = 0 (取 a=1)
        cartesian_leaf = ImplicitFunction(
            lambda x, y: x**3 + y**3 - 3 * x * y,  # F(x,y) = x³ + y³ - 3xy
            x_range=[-2, 2],
            y_range=[-2, 2],
            color=BLUE,
            stroke_width=3,
        )

        self.add(cartesian_leaf)

笛卡尔叶形线 (一种自交曲线),可以展示隐函数处理 多值曲线 的能力(一个x对应多个y)。

3. ParametricFunction

  • 用途 :绘制 参数方程 定义的曲线(如螺旋线、摆线)。
  • 输入要求
    接受一个 向量值函数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( t ) = ( x ( t ) , y ( t ) ) \mathbf{r}(t) = (x(t), y(t)) </math>r(t)=(x(t),y(t))) 和参数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t) 的范围(如t_range=[0, T])。
  • 特点
    • 通过参数 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t) 映射到点 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( x ( t ) , y ( t ) ) (x(t), y(t)) </math>(x(t),y(t))。
    • 可绘制 任意参数化曲线(包括封闭曲线、自交曲线)。
  • 示例代码
python 复制代码
class ParametricFunctionExample(Scene):
    def construct(self):
        # 四叶玫瑰线参数方程: r = sin(2θ) 的笛卡尔形式
        # x = sin(2t)cos(t), y = sin(2t)sin(t)
        rose_curve = ParametricFunction(
            lambda t: np.array(
                [
                    np.sin(2 * t) * np.cos(t),  # x(t)
                    np.sin(2 * t) * np.sin(t),  # y(t)
                    0,
                ]
            ),
            t_range=[0, 2 * PI],  # 完整周期
            color=RED,
            stroke_width=4,
        )

        self.add(rose_curve)

四叶玫瑰线 展示参数方程处理 封闭曲线 的能力,通过参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t直接控制曲线生成过程。

4. 核心区别总结

特性 FunctionGraph ImplicitFunction ParametricFunction
输入形式 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = f ( x ) y = f(x) </math>y=f(x) <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0 <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( t ) = ( x ( t ) , y ( t ) ) \mathbf{r}(t) = (x(t), y(t)) </math>r(t)=(x(t),y(t))
函数类型 显式函数(单值) 隐函数(多值) 参数方程
适用场景 简单函数(如 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = sin ⁡ x ) y = \sin x) </math>y=sinx) 复杂曲线(如椭圆、心形线) 任意参数化曲线(如螺旋线)
计算复杂度 低(直接计算) 高(数值求解) 中(采样计算)
多值支持 ❌ 不支持 ✔️ 支持 ✔️ 支持

5. 如何选择?

  • FunctionGraph 当函数能显式写成 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = f ( x ) y = f(x) </math>y=f(x)时(如多项式、三角函数)。
  • ImplicitFunction 当函数以方程 <math xmlns="http://www.w3.org/1998/Math/MathML"> F ( x , y ) = 0 F(x, y) = 0 </math>F(x,y)=0 给出时(如圆、椭圆)。
  • ParametricFunction 当曲线用参数 ( t ) 描述时(如摆线、贝塞尔曲线)。

通过理解这些差异,你可以根据函数的具体形式高效选择对应的 Manim 类进行绘制。

相关推荐
love530love8 分钟前
【保姆级教程】阿里 Wan2.1-T2V-14B 模型本地部署全流程:从环境配置到视频生成(附避坑指南)
人工智能·windows·python·开源·大模型·github·音视频
He19550139 分钟前
Go初级之十:错误处理与程序健壮性
开发语言·python·golang
和鲸社区1 小时前
《斯坦福CS336》作业1开源,从0手搓大模型|代码复现+免环境配置
人工智能·python·深度学习·计算机视觉·语言模型·自然语言处理·nlp
豌豆花下猫2 小时前
Python 潮流周刊#118:Python 异步为何不够流行?(摘要)
后端·python·ai
THMAIL2 小时前
深度学习从入门到精通 - LSTM与GRU深度剖析:破解长序列记忆遗忘困境
人工智能·python·深度学习·算法·机器学习·逻辑回归·lstm
wheeldown2 小时前
【数学建模】数据预处理入门:从理论到动手操作
python·数学建模·matlab·python3.11
多打代码3 小时前
2025.09.05 用队列实现栈 & 有效的括号 & 删除字符串中的所有相邻重复项
python·算法
@CLoudbays_Martin113 小时前
为什么动态视频业务内容不可以被CDN静态缓存?
java·运维·服务器·javascript·网络·python·php
程序猿炎义3 小时前
【NVIDIA AIQ】自定义函数实践
人工智能·python·学习
THMAIL4 小时前
深度学习从入门到精通 - BERT与预训练模型:NLP领域的核弹级技术详解
人工智能·python·深度学习·自然语言处理·性能优化·bert