先放题目
HAMMERATH表现
from manim import *
import numpy as np
class FunctionAnalysis(Scene):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Tex.set_default(tex_template=TexTemplateLibrary.ctex)
def construct(self):
# 创建左右分区(隐形边框)
left_region = Rectangle(width=7.5, height=7.0).to_edge(LEFT, buff=0.2)
right_region = Rectangle(width=5.5, height=7.0).to_edge(RIGHT, buff=0.2)
# 题目展示
title = Tex(r"\text{已知函数 } f(x) = \frac{x}{e^x}", font_size=36).move_to(left_region.get_top() + DOWN * 0.5)
question = Tex(r"\text{则下列说法正确的是 (ACD)}", font_size=32).next_to(title, DOWN, buff=0.3)
self.play(Write(title))
self.play(Write(question))
self.wait(1)
# ==================== 选项A分析 ====================
option_a = Tex(r"\text{A. } f(x) \text{ 的单调递减区间是 } [1, +\infty)", font_size=32).next_to(question, DOWN, buff=0.4)
self.play(Write(option_a))
self.wait(0.5)
# 右侧绘制函数图像
axes_a = Axes(
x_range=[-1.0, 5.0, 1.0],
y_range=[-0.2, 0.5, 0.1],
x_length=4.5,
y_length=3.5,
axis_config={"include_tip": True, "font_size": 24}
).move_to(right_region.get_center() + UP * 1.0)
# 函数 f(x) = x/e^x
def func(x):
return x / np.exp(x)
graph_a = axes_a.plot(func, x_range=[-0.5, 5.0], color=BLUE)
self.play(Create(axes_a))
self.play(Create(graph_a))
self.wait(0.5)
# 标记单调递减区间 [1, +∞)
critical_point = Dot(axes_a.c2p(1.0, func(1.0)), color=RED)
decreasing_line = axes_a.plot(func, x_range=[1.0, 5.0], color=RED, stroke_width=6)
self.play(Create(critical_point))
self.play(Create(decreasing_line))
self.wait(0.5)
result_a = Tex(r"\text{A 正确}", font_size=32, color=GREEN).next_to(option_a, DOWN, buff=0.3)
self.play(Write(result_a))
self.wait(1)
# ==================== 选项B分析 ====================
option_b = Tex(r"\text{B. 若 } m < \frac{1}{e}\text{,则方程 } f(x) = m \text{ 有两个不等实根}", font_size=28)
option_b.move_to(left_region.get_center() + UP * 1.5)
self.play(
FadeOut(title),
FadeOut(question),
FadeOut(option_a),
FadeOut(result_a),
Write(option_b)
)
self.wait(0.5)
# 清除右侧内容
self.play(
FadeOut(axes_a),
FadeOut(graph_a),
FadeOut(critical_point),
FadeOut(decreasing_line)
)
# 重新绘制坐标系
axes_b = Axes(
x_range=[-1.0, 6.0, 1.0],
y_range=[-0.1, 0.5, 0.1],
x_length=4.5,
y_length=3.5,
axis_config={"include_tip": True, "font_size": 24}
).move_to(right_region.get_center() + UP * 0.5)
graph_b = axes_b.plot(func, x_range=[-0.5, 6.0], color=BLUE)
self.play(Create(axes_b))
self.play(Create(graph_b))
self.wait(0.5)
# f(x)的最大值在x=1处,f(1) = 1/e ≈ 0.368
max_val = 1.0 / np.e
# 绘制 0 < m < 1/e 的区域(用条纹表示)
stripe_lines = []
for m_val in np.linspace(0.05, max_val - 0.02, 8):
h_line = axes_b.plot(lambda x, mv=m_val: mv, x_range=[-0.5, 6.0], color=GREEN, stroke_width=1)
h_line.set_opacity(0.5)
stripe_lines.append(h_line)
stripe_group = VGroup(*stripe_lines)
self.play(LaggedStart(*[Create(line) for line in stripe_lines], lag_ratio=0.1))
self.wait(0.5)
# 但是当 m=0 时只有一个根(x=0),所以B错误
analysis_b = Tex(r"\text{当 } m = 0 \text{ 时只有一个根}", font_size=28).next_to(option_b, DOWN, buff=0.3)
result_b = Tex(r"\text{B 错误}", font_size=32, color=RED).next_to(analysis_b, DOWN, buff=0.2)
self.play(Write(analysis_b))
self.play(Write(result_b))
self.wait(1)
# ==================== 选项C分析 ====================
option_c = Tex(r"\text{C. 点 P 到直线 } y = x + 2 \text{ 距离最小值为 } \sqrt{2}", font_size=28)
option_c.move_to(left_region.get_center() + UP * 2.0)
self.play(
FadeOut(option_b),
FadeOut(analysis_b),
FadeOut(result_b),
Write(option_c)
)
self.wait(0.5)
# 清除右侧
self.play(
FadeOut(axes_b),
FadeOut(graph_b),
FadeOut(stripe_group)
)
# 重新绘制
axes_c = Axes(
x_range=[-1.0, 5.0, 1.0],
y_range=[-1.0, 3.0, 1.0],
x_length=4.5,
y_length=4.0,
axis_config={"include_tip": True, "font_size": 24}
).move_to(right_region.get_center())
graph_c = axes_c.plot(func, x_range=[-0.5, 5.0], color=BLUE)
self.play(Create(axes_c))
self.play(Create(graph_c))
self.wait(0.5)
# 绘制直线 y = x + 2
target_line = axes_c.plot(lambda x: x + 2.0, x_range=[-1.0, 2.0], color=GREEN, stroke_width=3)
self.play(Create(target_line))
self.wait(0.5)
x_tangent = 0.0
y_tangent = func(x_tangent)
tangent_point = Dot(axes_c.c2p(x_tangent, y_tangent), color=RED)
# 切线方程:y = x
tangent_line = axes_c.plot(lambda x: x, x_range=[-0.5, 2.0], color=RED, stroke_width=4)
self.play(Create(tangent_point))
self.play(Create(tangent_line))
self.wait(0.5)
calc_c1 = Tex(r"\text{切线斜率为 1 时:} f'(x) = 1", font_size=28).next_to(option_c, DOWN, buff=0.3)
calc_c2 = Tex(r"\text{切线:} y = x", font_size=28).next_to(calc_c1, DOWN, buff=0.2)
calc_c3 = Tex(r"d = \frac{|2|}{\sqrt{2}} = \sqrt{2}", font_size=28).next_to(calc_c2, DOWN, buff=0.2)
result_c = Tex(r"\text{C 正确}", font_size=32, color=GREEN).next_to(calc_c3, DOWN, buff=0.2)
self.play(Write(calc_c1))
self.play(Write(calc_c2))
self.play(Write(calc_c3))
self.play(Write(result_c))
self.wait(1)
# ==================== 选项D分析 ====================
option_d = Tex(r"\text{D. 过点 } A(0, a) \text{ 可作三条切线,则 } 0 < a < \frac{4}{e^2}", font_size=26)
option_d.move_to(left_region.get_center() + UP * 2.5)
self.play(
FadeOut(option_c),
FadeOut(calc_c1),
FadeOut(calc_c2),
FadeOut(calc_c3),
FadeOut(result_c),
Write(option_d)
)
self.wait(0.5)
# 清除右侧
self.play(
FadeOut(axes_c),
FadeOut(graph_c),
FadeOut(target_line),
FadeOut(tangent_point),
FadeOut(tangent_line)
)
# 重新绘制
axes_d = Axes(
x_range=[-1.0, 6.0, 1.0],
y_range=[-0.2, 0.5, 0.1],
x_length=4.5,
y_length=3.5,
axis_config={"include_tip": True, "font_size": 24}
).move_to(right_region.get_center() + UP * 0.8)
graph_d = axes_d.plot(func, x_range=[-0.5, 6.0], color=BLUE)
self.play(Create(axes_d))
self.play(Create(graph_d))
self.wait(0.5)
inflection_x = 2.0
inflection_y = func(inflection_x)
inflection_point = Dot(axes_d.c2p(inflection_x, inflection_y), color=RED, radius=0.08)
self.play(Create(inflection_point))
self.wait(0.5)
slope_at_inflection = -1.0 / (np.e ** 2)
y_intercept = inflection_y - inflection_x * slope_at_inflection
intercept_point = Dot(axes_d.c2p(0.0, y_intercept), color=GREEN, radius=0.08)
tangent_at_inflection = axes_d.plot(
lambda x: slope_at_inflection * x + y_intercept,
x_range=[-0.5, 5.0],
color=RED,
stroke_width=3
)
self.play(Create(tangent_at_inflection))
self.play(Create(intercept_point))
self.wait(0.5)
calc_d1 = Tex(r"f''(x) = \frac{x - 2}{e^x}", font_size=28).next_to(option_d, DOWN, buff=0.3)
calc_d2 = Tex(r"\text{拐点:} x = 2", font_size=28).next_to(calc_d1, DOWN, buff=0.2)
calc_d3 = Tex(r"\text{切线与 y 轴交点:} y = \frac{4}{e^2}", font_size=28).next_to(calc_d2, DOWN, buff=0.2)
result_d = Tex(r"\text{D 正确}", font_size=32, color=GREEN).next_to(calc_d3, DOWN, buff=0.2)
self.play(Write(calc_d1))
self.play(Write(calc_d2))
self.play(Write(calc_d3))
self.play(Write(result_d))
self.wait(2)
# 最终结论
final_answer = Tex(r"\text{答案:ACD}", font_size=40, color=GREEN).move_to(left_region.get_center())
self.play(
FadeOut(option_d),
FadeOut(calc_d1),
FadeOut(calc_d2),
FadeOut(calc_d3),
FadeOut(result_d),
Write(final_answer)
)
self.wait(2)