diff options
author | Padmapriya Mohan | 2020-06-25 17:31:36 +0530 |
---|---|---|
committer | GitHub | 2020-06-25 17:31:36 +0530 |
commit | d72ba1b05700096a2c42e9616e30a939e9b921a6 (patch) | |
tree | c1ba6c81f0c4fd5c9367cea2e0435971a1f59f4b /FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that | |
parent | fe5e18510140b3e02f3f6f03ad449c218f1b8579 (diff) | |
parent | c36313078049f0f94fca6229b434bffefd323e95 (diff) | |
download | FSF-mathematics-python-code-archive-d72ba1b05700096a2c42e9616e30a939e9b921a6.tar.gz FSF-mathematics-python-code-archive-d72ba1b05700096a2c42e9616e30a939e9b921a6.tar.bz2 FSF-mathematics-python-code-archive-d72ba1b05700096a2c42e9616e30a939e9b921a6.zip |
Merge pull request #1 from mohan-padmapriya/edit_two
Code for subtopic one: Gradient
Diffstat (limited to 'FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that')
7 files changed, 382 insertions, 0 deletions
diff --git a/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file1_missile-example.py b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file1_missile-example.py new file mode 100644 index 0000000..cd754cd --- /dev/null +++ b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file1_missile-example.py @@ -0,0 +1,37 @@ +from manimlib.imports import * +import numpy as np + + +def function(coordinate): + x,y = coordinate[:2] + return np.array([ + np.sin(x-y), + np.exp(y), + 0 + ]) +def func(coordinate): + x,y = coordinate[:2] + return np.array([ + -2*x, + y, + 0]) + +class Missiles(GraphScene): + def construct(self): + + field = VectorField(function) + #path = ParametricFunction(lambda x: -2*x) + + dot = SVGMobject("miss").move_to(DL).scale(0.09).set_color(WHITE).rotate(PI/4 + PI) + path = ArcBetweenPoints(dot.get_center(), UP+0.2*LEFT) + + self.play(FadeIn(field)) + self.wait() + self.play(FadeIn(dot)) + self.wait() + self.play(MoveAlongPath(dot, path)) + self.play(ApplyMethod(dot.rotate, PI/4), rate = 0.2) + self.play(ApplyMethod(dot.move_to, 3.5*UP), rate = 0.3) + #self.play(ApplyMethod(dot.move_to, 3.5*UP)) + #self.add_fixed_in_frame_mobjects(text_field) + self.wait()
\ No newline at end of file diff --git a/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file2_gradient-example-1.py b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file2_gradient-example-1.py new file mode 100644 index 0000000..7a95867 --- /dev/null +++ b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file2_gradient-example-1.py @@ -0,0 +1,43 @@ +from manimlib.imports import * +import numpy as np + + +def function(coordinate): + x,y = coordinate[:2] + return np.array([ + 0.4*x, + 0.4*y, + 0.4*np.cos(np.sqrt((x**2)+(y**2)) + )]) + +class ThreeDVector(ThreeDScene): + def construct(self): + axes = ThreeDAxes() + self.add(axes) + self.set_camera_orientation(phi=45*DEGREES,theta=60*DEGREES,distance=40) + self.begin_ambient_camera_rotation(rate=0.5) + + surface = ParametricSurface( + lambda u, v: np.array([ + 0.4*u, + 0.4*v, + 0.4*np.cos(np.sqrt((u**2)+(v**2))) + ]),u_min=-20,u_max=20, v_min=-10,v_max=10).set_color(BLUE_E).fade(0.7) + + text_func = TexMobject(r"\textbf{Input: Function}").shift(4.4*LEFT+3.3*UP).scale(0.7) + text_field = TexMobject(r"\textbf{Output: Vector Field}").shift(4.4*LEFT+3.3*UP).scale(0.7) + + + self.add_fixed_in_frame_mobjects(text_func) + self.play(ShowCreation(surface)) + self.wait(3) + + field = VectorField(function) + self.play(FadeIn(field), FadeOut(text_func)) + self.add_fixed_in_frame_mobjects(text_field) + self.wait() + self.play(FadeOut(surface)) + self.wait() + + + diff --git a/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file3_gradient-example-2.py b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file3_gradient-example-2.py new file mode 100644 index 0000000..e37581d --- /dev/null +++ b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file3_gradient-example-2.py @@ -0,0 +1,39 @@ +from manimlib.imports import * +import numpy as np + + +def function(coordinate): + x,y = coordinate[:2] + return np.array([ + x, + y, + x**2 - y**2 + ]) + +class ThreeDVector_three(ThreeDScene): + def construct(self): + axes = ThreeDAxes() + self.add(axes) + self.set_camera_orientation(phi=45*DEGREES,theta=85*DEGREES,distance=40) + self.begin_ambient_camera_rotation(rate=0.5) + + surface = ParametricSurface( + lambda x, y: np.array([ + x, + y, + x**2 - y**2 + ]),u_min=-2,u_max=2, v_min=-1.5,v_max=1.5).set_color(BLUE_E).fade(0.7).scale(1.7) + + text_func = TexMobject(r"\textbf{Input: Function}").shift(4.4*LEFT+3.3*UP).scale(0.7) + text_field = TexMobject(r"\textbf{Output: Vector Field}").shift(4.4*LEFT+3.3*UP).scale(0.7) + + self.add_fixed_in_frame_mobjects(text_func) + self.play(ShowCreation(surface)) + self.wait(3) + + field = VectorField(function) + self.play(FadeIn(field), FadeOut(text_func)) + self.add_fixed_in_frame_mobjects(text_field) + self.wait() + self.play(FadeOut(surface)) + self.wait(2)
\ No newline at end of file diff --git a/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file4-multidimensional-gradient.py b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file4-multidimensional-gradient.py new file mode 100644 index 0000000..e9c8fd0 --- /dev/null +++ b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file4-multidimensional-gradient.py @@ -0,0 +1,113 @@ +from manimlib.imports import * +import numpy as np + + +class Hills(ThreeDScene): + def construct(self): + axes = ThreeDAxes( + number_line_config={ + "color": GREEN, + "include_tip": False, + "exclude_zero_from_default_numbers": True, + } + ) + self.add(axes) + + self.set_camera_orientation(phi=45*DEGREES,theta=45*DEGREES,distance=40) + #self.begin_ambient_camera_rotation(rate=0.5) + self.wait() + + cylinder_1 = ParametricSurface( + lambda u, v: np.array([ + u, + v, + 7*u*v/np.exp(u**2+v**2) + ]),u_min=-3,u_max=3, v_min=-1,v_max=-0.95).set_color(YELLOW_E).rotate(PI/2).shift(LEFT).fade(0.5) + cylinder = ParametricSurface( + lambda u, v: np.array([ + u, + v, + 7*u*v/np.exp(u**2+v**2) + ]),u_min=-3,u_max=3, v_min=-3,v_max=3).set_color(YELLOW_E).rotate(PI/2).shift(LEFT).fade(0.5) + text_one = TexMobject(r"\textrm{Single variable functions slope up and down}") + #name = TexMobject(r"\textrm{PROBE}").next_to(text_one, DOWN, buff = SMALL_BUFF).scale(0.7) + text_one_a = TexMobject(r"\textrm{Position }", r" \rightarrow ").next_to(text_one, DOWN, buff = SMALL_BUFF) + probe = Sphere(radius = 0.2).next_to(text_one_a, RIGHT).set_color(BLUE_E) + text_one_b = TexMobject(r" \rightarrow ", r"\textrm{ slope }").next_to(probe, RIGHT, buff = SMALL_BUFF) + name = TextMobject("PROBE").next_to(probe, DOWN, buff = SMALL_BUFF).scale(0.5) + text = VGroup(text_one, text_one_a, probe, text_one_b, name).to_edge(UP+1.5*LEFT).scale(0.5) + + text_two = TexMobject(r"\textrm{Multivariable functions slope in infinitely many directions!}") + #name_two = TexMobject(r"\textrm{PROBE2.0}").next_to(text_two, DOWN, buff = SMALL_BUFF).scale(0.7) + text_two_a = TexMobject(r"\textrm{Position, Direction }", r" \rightarrow ").next_to(text_two, DOWN, buff = SMALL_BUFF) + probe_two = Sphere(radius = 0.2).next_to(text_two_a, RIGHT).set_color(PURPLE_E) + text_two_b = TexMobject(r" \rightarrow ", r"\textrm{ slope }").next_to(probe_two, RIGHT, buff = SMALL_BUFF) + name_two = TextMobject("PROBE2.0").next_to(probe_two, DOWN, buff = SMALL_BUFF).scale(0.5) + two = VGroup(text_two, text_two_a, probe_two, text_two_b, name_two).to_edge(UP+LEFT).scale(0.5).shift(3.5*LEFT) + + + + + + + self.play(ShowCreation(cylinder_1)) + self.wait() + self.add_fixed_in_frame_mobjects(text) + self.wait(3.5) + self.play(FadeOut(text)) + self.play(ReplacementTransform(cylinder_1, cylinder)) + self.wait() + self.add_fixed_in_frame_mobjects(two) + self.begin_ambient_camera_rotation(rate=0.5) + self.wait(4) + +class OneMore(ThreeDScene, GraphScene): + def setup(self): + GraphScene.setup(self) + ThreeDScene.setup(self) + + def construct(self): + axes = ThreeDAxes( + number_line_config={ + "color": GREEN, + "include_tip": False, + "exclude_zero_from_default_numbers": True, + } + ) + self.add(axes) + + self.set_camera_orientation(phi=90*DEGREES,theta=90*DEGREES,distance=40) + #self.begin_ambient_camera_rotation(rate=0.5) + self.wait() + + shape = ParametricSurface( + lambda u, v: np.array([ + u, + v, + 2 - u**2 - v**2, + ]),u_min=0,u_max =0.01, v_min=-2,v_max=2).set_color(GREEN_C) + + shape_A = ParametricSurface( + lambda u, v: np.array([ + 0*u, + 0, + v, + ]),u_min=-2,u_max = 2, v_min=-2,v_max=2).set_color(GREEN_C) + + + ''' + path = self.get_graph(lambda u,v: np.array([ + u, + 2 - u**2 - v**2]), u_min=-2,u_max=2, v_min=-2,v_max=2) + location = np.array([-2,-2, -2]) #location: Point + dot = Dot(location) + ''' + + self.play(ShowCreation(shape)) + self.add(shape_A) + #self.play(ShowCreation(path), ShowCreation(dot)) + #self.play(MoveAlongPath(dot, path)) + #self.wait(3) + self.play(ApplyMethod(shape.fade, 0.5)) + self.begin_ambient_camera_rotation(rate = 0.5) + self.wait(3) diff --git a/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file4_gradient-example-3.py b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file4_gradient-example-3.py new file mode 100644 index 0000000..7c0ef54 --- /dev/null +++ b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file4_gradient-example-3.py @@ -0,0 +1,40 @@ +from manimlib.imports import * +import numpy as np + + +def function(coordinate): + x,y = coordinate[:2] + return np.array([ + x, + y, + 1/np.cos(x*y), + ]) + +class ThreeDVector(ThreeDScene): + def construct(self): + axes = ThreeDAxes() + self.add(axes) + self.set_camera_orientation(phi=45*DEGREES,theta=45*DEGREES,distance=40) + self.begin_ambient_camera_rotation(rate=0.5) + + surface = ParametricSurface( + lambda x, y: np.array([ + x, + y, + 1/np.cos(x*y), + ]),u_min=-1.15,u_max=1.15, v_min=-1.15,v_max=1.15).set_color(BLUE_E).fade(0.7).scale(1.4) + + text_func = TexMobject(r"\textbf{Input: Function}").shift(4.4*LEFT+3.3*UP).scale(0.3) + text_field = TexMobject(r"\textbf{Output: Vector Field}").shift(4.4*LEFT+3.3*UP).scale(0.7) + + self.add_fixed_in_frame_mobjects(text_func) + self.play(ShowCreation(surface)) + self.wait(2) + + field = VectorField(function).scale(0.7) + self.play(FadeIn(field), FadeOut(text_func)) + self.add_fixed_in_frame_mobjects(text_field) + self.wait() + + self.play(FadeOut(surface)) + self.wait(2) diff --git a/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file5_steepest-ascent-analogy.py b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file5_steepest-ascent-analogy.py new file mode 100644 index 0000000..803ea4a --- /dev/null +++ b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file5_steepest-ascent-analogy.py @@ -0,0 +1,71 @@ +from manimlib.imports import * +import numpy as np + +class Rules(ThreeDScene): + + def setup(self): + ThreeDScene.setup(self) + + def construct(self): + axes = ThreeDAxes()#.scale(0.8)#.shift(2*RIGHT, 2*DOWN) + self.set_camera_orientation(phi=PI/3 + 10*DEGREES,theta=-PI/4 + 25*DEGREES,distance=60) + + plane = Polygon(np.array([4,4,0]), np.array([4, -4, 0]), np.array([-4, -4, 0]), np.array([-4, 4, 0]), color = BLACK, fill_color = YELLOW_E, fill_opacity = 0.3) + + path_one = Line(start = ORIGIN, end = np.array([3, 0, 0])).set_color(BLUE_E) + path_one_text = TexMobject(r"\textrm{3 steps in the x direction}").shift(2*DOWN+3.5*LEFT).scale(0.5).set_color(BLUE_E) + path_two_text = TexMobject(r"\textrm{2 steps in the y direction}").next_to(path_one_text, DOWN, buff = SMALL_BUFF).scale(0.5).set_color(GREEN_C) + path_three_text = TexMobject(r"\textrm{2 steps in the z direction}").next_to(path_two_text, DOWN, buff = SMALL_BUFF).scale(0.5).set_color(PURPLE_E) + obj = TextMobject("Objective: ", "Maximise points for a set number of steps").scale(0.8).to_edge(UP+LEFT) + + steps = TexMobject(r"\textrm{Total steps = 7}").to_edge(RIGHT, buff = LARGE_BUFF).set_color(YELLOW_E).scale(0.7).shift(1*UP) + points_one = TexMobject(r"\textrm{Total points = 13}").next_to(steps, DOWN, buff = SMALL_BUFF).scale(0.7).set_color(YELLOW_E) + question = TexMobject(r"\textrm{Change direction to score more points?}").shift(2*DOWN+2.5*RIGHT).scale(0.9) + + path_two = Line(start = np.array([3, 0, 0]), end = np.array([3, 2, 0])).set_color(GREEN_C) + path_three = Line(start = np.array([3, 2, 0]), end = np.array([3, 2, 2])).set_color(PURPLE_E) + total = Line(start = np.array([0, 0, 0]), end = np.array([3, 2, 2])).set_color(YELLOW_E) + projection_total = Line(start = np.array([0, 0, 0]), end = np.array([3, 2, 0])) + paths = VGroup(path_one, path_two, path_three, path_one_text, path_two_text, path_three_text) + + total_one = VGroup(total, projection_total) + + total_two = Line(start = np.array([0, 0, 0]), end = np.array([4, 1, 2])).set_color(YELLOW_E) + projection_total_two = Line(start = np.array([0, 0, 0]), end = np.array([4, 1, 0])) + points_two = TexMobject(r"\textrm{Total points = 12}").next_to(steps, DOWN, buff = SMALL_BUFF).scale(0.7).set_color(YELLOW_E) + + total_three = Line(start = np.array([0, 0, 0]), end = np.array([1, 4, 2])).set_color(YELLOW_E) + projection_total_three = Line(start = np.array([0, 0, 0]), end = np.array([1, 4, 0])) + points_three = TexMobject(r"\textrm{Total points = 15}").next_to(steps, DOWN, buff = SMALL_BUFF).scale(0.7).set_color(YELLOW_E) + + everything = VGroup(axes, plane, path_one, path_two, path_three, total, projection_total, total_two, projection_total_two, total_three, projection_total_three) + everything.scale(0.7).shift(2*LEFT+1*DOWN) + self.add_fixed_in_frame_mobjects(obj) + self.add(axes, plane) + self.wait() + self.add_fixed_in_frame_mobjects(path_one_text) + self.play(ShowCreation(path_one)) + self.wait() + self.add_fixed_in_frame_mobjects(path_two_text) + self.play(ShowCreation(path_two)) + self.wait() + self.add_fixed_in_frame_mobjects(path_three_text) + self.play(ShowCreation(path_three)) + self.add_fixed_in_frame_mobjects(steps, points_one) + self.wait() + self.play(ShowCreation(total)) + self.play(ReplacementTransform(total.copy(), projection_total)) + self.wait() + self.play(FadeOut(paths)) + self.add_fixed_in_frame_mobjects(question) + self.wait() + self.play(ReplacementTransform(total, total_two), ReplacementTransform(projection_total, projection_total_two)) + self.play(FadeOut(points_one)) + self.add_fixed_in_frame_mobjects(points_two) + self.wait() + self.play(ReplacementTransform(total_two, total_three), ReplacementTransform(projection_total_two, projection_total_three)) + self.play(FadeOut(points_two)) + self.add_fixed_in_frame_mobjects(points_three) + self.wait() + + diff --git a/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file6-maximum.py b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file6-maximum.py new file mode 100644 index 0000000..255a0eb --- /dev/null +++ b/FSF-2020/calculus-of-several-variables/div-curl-grad-and-all-that/gradient/file6-maximum.py @@ -0,0 +1,39 @@ +from manimlib.imports import * +import numpy as np + + +def function(coordinate): + x,y = coordinate[:2] + return np.array([ + x, + y, + 2 - x**2 - y**2 + ]) + +class ThreeDVector_one(ThreeDScene): + def construct(self): + axes = ThreeDAxes() + self.add(axes) + self.set_camera_orientation(phi=75*DEGREES,theta=45*DEGREES,distance=40) + self.begin_ambient_camera_rotation(rate=0.5) + + surface = ParametricSurface( + lambda x, y: np.array([ + x, + y, + 2 - x**2 - y**2 + ]),u_min=-2,u_max=2, v_min=-2,v_max=2).set_color(RED_E).fade(0.7) + + probe = Sphere(radius = 0.2).set_color(PURPLE_E) + probe_one = Sphere(radius = 0.1).set_color(PURPLE_E).move_to(np.array([0,0,2.1])) + text_one_b = TexMobject(r" \textrm{ reads 0 at local maximum }").next_to(probe, RIGHT, buff = SMALL_BUFF) + name = TextMobject("PROBE2.0 ").next_to(probe, DOWN, buff = SMALL_BUFF).scale(0.5) + text = VGroup(probe, text_one_b, name).to_edge(1.5*UP+0.5*LEFT).scale(0.5) + + + self.play(ShowCreation(surface)) + self.add(probe_one) + field = VectorField(function).scale(0.8) + self.play(FadeIn(field)) + self.add_fixed_in_frame_mobjects(text) + self.wait(3) |