Last active
April 13, 2020 23:03
-
-
Save marcodebe/e1a25e3b0fcc7a102b26b0737b819d07 to your computer and use it in GitHub Desktop.
Taylor for sin(x) animation in manim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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