Skip to content

Instantly share code, notes, and snippets.

@chiangqiqi
Created July 24, 2019 03:07
Show Gist options
  • Save chiangqiqi/caf0344cfa52098617d2a850d157678b to your computer and use it in GitHub Desktop.
Save chiangqiqi/caf0344cfa52098617d2a850d157678b to your computer and use it in GitHub Desktop.
2019 高考理科20题
from manimlib.imports import *
class Playground(Scene):
def construct(self):
semicircle = Arc(fill_opacity=1, angle=PI/2)
self.play(FadeIn(semicircle))
def Range(in_val,end_val,step=1):
return list(np.arange(in_val,end_val+step,step))
class PlotGK1(GraphScene):
CONFIG = {
"y_max" : 1.5,
"y_min" : -1.5,
"x_max" : 10,
"x_min" : -1,
"y_tick_frequency" : 0.5,
"x_tick_frequency" : 0.5,
"graph_origin" : ORIGIN,
"y_axis_label": None, # Don't write y axis label
"x_axis_label": None,
}
def construct(self):
self.setup_axes()
plotf = self.get_graph(lambda x : np.sin(x) - np.log(x+1),
color = GREEN,
x_min=-0.8,
x_max=8,
)
plotdf = self.get_graph(lambda x : np.cos(x) - 1/(x+1),
color = GRAY,
x_min=-0.8,
x_max=8,
)
plotf.set_stroke(width=3) # width of line
plotdf.set_stroke(width=2)
# Animation
e1 = TexMobject("f(x) = sin(x) - ln(1+x)").to_edge(LEFT)
self.play(FadeIn(e1))
self.play(FadeOut(e1))
self.play(ShowCreation(plotf), run_time = 2)
self.wait()
e2 = TexMobject("f'(x) = cos(x) - \\frac{1}{1+x}").to_edge(LEFT)
self.play(FadeIn(e2))
self.play(FadeOut(e2))
self.wait()
self.play(ShowCreation(plotdf), run_time = 2)
self.wait()
# ------------------- play problem 1
e2 = TextMobject("(1) $f'(x)$ 在区间 $(-1, \\pi/2)$ \\\\ 中只有一个极值点").to_edge(LEFT)
self.play(FadeIn(e2))
self.wait(3)
self.play(FadeOut(e2))
# the first problem cares nothing about f(x)
self.play(FadeOut(plotf))
# blines = self.get_vertical_line_to_graph(-1 , plotdf,
# line_class=DashedLine)
# self.play(ShowCreation(boundary_lines))
e4 = TextMobject("可以看出 $f'(x)$ 在区间内为凸").to_edge(LEFT).to_edge(UP)
self.play(ShowCreation(e4))
self.wait(4)
e5 = TextMobject("令 $g(x) = f'(x)$ ")
e5.next_to(e4, DOWN)
self.play(ShowCreation(e5))
e61 = TexMobject("g'(x) = -sin(x) + \\frac{1}{(1+x^2)}").to_edge(LEFT)
e61.next_to(e5, DOWN)
e62 = TexMobject("g''(x) = -cos(x) - \\frac{2x}{(1+x^2)^2} < 0 ").to_edge(LEFT)
e62.next_to(e61, DOWN)
self.play(ShowCreation(e61))
self.play(ShowCreation(e62))
self.wait(4)
e7 = TextMobject("$g'(x) > 0 , -1<x<0$ \\\\ $g'(\\frac{\\pi}{2}) < 0$ ").to_edge(LEFT)
e7.next_to(e62, DOWN)
self.play(ShowCreation(e7))
e8 = TextMobject("证毕").to_edge(LEFT)
e8.next_to(e7, DOWN)
self.play(ShowCreation(e8))
def setup_axes(self):
GraphScene.setup_axes(self)
# width of edges
self.x_axis.set_stroke(width=2)
self.y_axis.set_stroke(width=2)
# color of edges
self.x_axis.set_color(RED)
self.y_axis.set_color(YELLOW)
# Add x,y labels
func = TexMobject("y")
var = TexMobject("x")
func.set_color(BLUE)
var.set_color(PURPLE)
func.next_to(self.y_axis,UP)
var.next_to(self.x_axis,RIGHT+UP)
# Y labels
self.y_axis.label_direction = LEFT*1.5
self.y_axis.add_numbers(*[-1,1])
#Parametters of x labels
init_val_x = -1
step_x = 1
end_val_x = 5
# List of the positions of x labels
values_decimal_x=Range(init_val_x,end_val_x,step_x)
# List of tex objects
list_x=TexMobject("-1", # -3pi/2
"0", # -pi
"1", # -pi/2
"2", # 0 (space)
"3", # pi/2
"4",# pi
"5" # 3pi/2
)
#List touples (position,label)
values_x = [(i,j)
for i,j in zip(values_decimal_x,list_x)
]
self.x_axis_labels = VGroup()
for x_val, x_tex in values_x:
x_tex.scale(0.7)
if x_val == -PI or x_val == PI: #if x is equals -pi or pi
x_tex.next_to(self.coords_to_point(x_val, 0), 2*DOWN) #Put 2*Down
else: # In another case
x_tex.next_to(self.coords_to_point(x_val, 0), DOWN)
self.x_axis_labels.add(x_tex)
self.play(
*[Write(objeto)
for objeto in [
self.y_axis,
self.x_axis,
self.x_axis_labels,
func,var
]
],
run_time=2
)
# 第二问
class PlotGK2(GraphScene):
CONFIG = {
"y_max" : 2,
"y_min" : -2,
"x_max" : 20,
"x_min" : -1,
"y_tick_frequency" : 0.5,
"x_tick_frequency" : 0.5,
"graph_origin" : ORIGIN,
"y_axis_label": None, # Don't write y axis label
"x_axis_label": None,
}
def construct(self):
self.setup_axes()
plotf = self.get_graph(lambda x : np.sin(x) - np.log(x+1),
color = GREEN,
x_min=-0.9,
x_max=18,
)
plotfup = self.get_graph(lambda x : 1 - np.log(x+1),
color = GREEN,
x_min=-0.9,
x_max=18,
)
plotdf = self.get_graph(lambda x : np.cos(x) - 1/(x+1),
color = GRAY,
x_min=-0.9,
x_max=18,
)
plotf.set_stroke(width=3) # width of line
plotdf.set_stroke(width=2)
# Animation
e1 = TexMobject("f(x) = sin(x) - ln(1+x)").to_edge(LEFT)
self.play(FadeIn(e1))
self.play(FadeOut(e1))
self.play(ShowCreation(plotf), run_time = 2)
self.wait()
e2 = TexMobject("f'(x) = cos(x) - \\frac{1}{1+x}").to_edge(LEFT)
self.play(FadeIn(e2))
self.play(FadeOut(e2))
self.wait()
self.play(ShowCreation(plotdf), run_time = 2)
self.wait()
# ------------------- play problem 1
e2 = TextMobject("(2) $f(x)$ 有且仅有两个零点 ").to_edge(LEFT)
self.play(FadeIn(e2))
self.wait(4)
self.play(FadeOut(e2))
# the first problem cares nothing about f(x)
self.play(FadeOut(plotdf))
# blines = self.get_vertical_line_to_graph(-1 , plotdf,
# line_class=DashedLine)
# self.play(ShowCreation(boundary_lines))
e4 = TextMobject(" $-1 \le sin(x) \le 1$").to_edge(LEFT).to_edge(UP)
self.play(ShowCreation(e4))
self.wait(4)
e5 = TexMobject("f(x) \le 1 - ln(1+x)").to_edge(LEFT)
e5.next_to(e4, DOWN)
self.play(ShowCreation(e5), ShowCreationThenDestruction(plotfup))
e6 = TextMobject(" $x+1 > e$ 时, $f(x) < 0$ ").scale(0.6).to_edge(LEFT)
e6.next_to(e5, DOWN)
self.play(ShowCreation(e6))
e7 = TextMobject(" $x > e-1$ 时, $f(x) < 0$ ").scale(0.6).to_edge(LEFT)
e7.next_to(e5, DOWN)
vline = lambda x: DashedLine(
self.coords_to_point(x, -10),
self.coords_to_point(x, 10),
stroke_width=2)
vlinef = vline(np.e-1)
vlinef.set_color(RED)
self.play(Transform(e6, e7), ShowCreation(vlinef))
self.wait(4)
e8 = TextMobject(" $f(\\frac{\\pi}{2}) = 1-ln(1+ \\frac{\\pi}{2}) >0$ ").scale(0.6).to_edge(LEFT)
e8.next_to(e7, DOWN)
vlinedf = vline(PI/2)
vlinedf.set_color(YELLOW)
self.play(ShowCreation(e8), ShowCreation(vlinedf))
self.wait(4)
e9 = TextMobject(" $f(e-1) <0$ ").to_edge(LEFT)
e9.next_to(e8, DOWN)
e10 = TextMobject("又 $f'(x) < 0 x$ , \\\\ $f(x)$ 在 $(\\pi/2 , e-1)$ \\\\ 中仅有一个零点 ").scale(0.7).to_edge(LEFT)
e10.next_to(e7, DOWN)
self.play(FadeOut(e8) , Transform(e9, e10))
self.wait(4)
e11 = TextMobject(" 易知 $f(x)$ 在 $(0, \\frac{\\pi}{2})$ 上先增后降").scale(0.5).to_edge(LEFT)
e11.next_to(e10, DOWN)
self.play( ShowCreation(e11))
e12= TextMobject(" $f(x) >0, x \in (0, \\frac{\\pi}{2})$ ").scale(0.5).to_edge(LEFT)
e12.next_to(e11, DOWN)
self.play( ShowCreation(e12))
self.wait(4)
e13= TextMobject("又 $f(0) = 0$, \\\\ $f(x)$ 仅有两个零点 ").scale(0.5).to_edge(LEFT)
e13.next_to(e12, DOWN)
self.play( ShowCreation(e13))
e14= TextMobject(" 证毕 ").scale(0.5).to_edge(LEFT)
e14.next_to(e13, DOWN)
self.play( ShowCreation(e14))
self.wait(5)
def setup_axes(self):
GraphScene.setup_axes(self)
# width of edges
self.x_axis.set_stroke(width=2)
self.y_axis.set_stroke(width=2)
# color of edges
self.x_axis.set_color(RED)
self.y_axis.set_color(YELLOW)
# Add x,y labels
func = TexMobject("y")
var = TexMobject("x")
func.set_color(BLUE)
var.set_color(PURPLE)
func.next_to(self.y_axis,UP)
var.next_to(self.x_axis,RIGHT+UP)
# Y labels
self.y_axis.label_direction = LEFT*1.5
self.y_axis.add_numbers(*[-1,1])
#Parametters of x labels
init_val_x = -1
step_x = 1
end_val_x = 5
# List of the positions of x labels
values_decimal_x=Range(init_val_x,end_val_x,step_x)
# List of tex objects
list_x=TexMobject("-1", # -3pi/2
"0", # -pi
"1", # -pi/2
"2", # 0 (space)
"3", # pi/2
"4",# pi
"5" # 3pi/2
)
#List touples (position,label)
values_x = [(i,j)
for i,j in zip(values_decimal_x,list_x)
]
self.x_axis_labels = VGroup()
for x_val, x_tex in values_x:
x_tex.scale(0.7)
if x_val == -PI or x_val == PI: #if x is equals -pi or pi
x_tex.next_to(self.coords_to_point(x_val, 0), 2*DOWN) #Put 2*Down
else: # In another case
x_tex.next_to(self.coords_to_point(x_val, 0), DOWN)
self.x_axis_labels.add(x_tex)
self.play(
*[Write(objeto)
for objeto in [
self.y_axis,
self.x_axis,
self.x_axis_labels,
func,var
]
],
run_time=2
)
class Last(GraphScene):
def construct(self):
e2 = TextMobject("谢谢观看")
self.play(ShowCreationThenFadeOut(e2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment