Skip to content

Instantly share code, notes, and snippets.

@marcodebe
Last active April 13, 2020 23:03
Show Gist options
  • Save marcodebe/e1a25e3b0fcc7a102b26b0737b819d07 to your computer and use it in GitHub Desktop.
Save marcodebe/e1a25e3b0fcc7a102b26b0737b819d07 to your computer and use it in GitHub Desktop.
Taylor for sin(x) animation in manim
# See https://github.com/3b1b/manim
# Animation: https://debe.galliera.it/pub/taylor.mp4
from manimlib.imports import *
import math
from functools import partial
class Graphing(GraphScene):
CONFIG = {
"x_min": -10,
"x_max": 10,
"y_min": -4,
"y_max": 4,
"graph_origin": ORIGIN,
"function_color": WHITE,
"axes_color": BLUE
}
def label(self, n):
lab = "y = x"
sign = "-"
for i in range(1, n+1):
k = 2*i + 1
lab += sign + "\\frac{x^{%s}}{%s!}" % (k, k)
if sign == "-":
sign = "+"
else:
sign = "-"
return lab
def taylor(self, n, x):
val = 0
sign = 1
for i in range(n+1):
k = 2*i + 1
val += sign * x**k / math.factorial(k)
sign *= -1
return val
def construct(self):
self.setup_axes(animate=True)
for i in range(10):
ptaylor = partial(self.taylor, i)
func_graph = self.get_graph(ptaylor, self.function_color)
graph_lab = self.get_graph_label(func_graph, label = self.label(i))
if i == 0:
self.play(ShowCreation(func_graph), Write(graph_lab))
old_graph = func_graph
old_lab = graph_lab
else:
self.play(Transform(old_graph, func_graph), Transform(old_lab, graph_lab))
self.wait(0.15)
self.wait(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment