summaryrefslogtreecommitdiff
path: root/FSF-2020/calculus-of-several-variables/multivariable-functions-and-paritial-derivatives/partial-derivatives/file4_partial_deriv_example.py
blob: 5712a62d6487bf22d33b04af1189caf90cc3f1ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from manimlib.imports import *

class PartialDerivX(ThreeDScene):
    def construct(self):
        axes = ThreeDAxes() 

        paraboloid = ParametricSurface(
            lambda u, v: np.array([
                2*np.sin(u)*np.cos(v),
                2*np.sin(u)*np.sin(v),
                -2*2*np.sin(u)*np.sin(u)+2
            ]),u_min=0,u_max=PI/2,v_min=0,v_max=2*PI,checkerboard_colors=[PINK, PURPLE],
            resolution=(15, 32)).scale(1)

        paraboloid_copy = paraboloid.copy()

 
        paraboloid_x = ParametricSurface(
            lambda u, v: np.array([
                2*np.sin(u)*np.cos(v),
                2*np.sin(u)*np.sin(v),
                -2*2*np.sin(u)*np.sin(u)+2
            ]),u_min=0,u_max=PI/2,v_min=PI,v_max=2*PI,checkerboard_colors=[PINK, PURPLE],
            resolution=(15, 32)).scale(1)


        parabola =ParametricFunction(
                lambda u : np.array([
                u,
                0,
                -(u*u) + 2
            ]),color="#006400",t_min=-2,t_max=2,
            )
        
        plane = Polygon(np.array([-2.2,0,-2.5]),np.array([2.2,0,-2.5]),np.array([2.2,0,2.5]),np.array([-2.2,0,2.5]),np.array([-2.2,0,-2.5]), color = GREEN, fill_color = GREEN, fill_opacity = 0.2)
        plane_text = TextMobject(r"$y = 0$", color = GREEN_C).move_to(2*UP + 3*RIGHT)

        surface_eqn = TextMobject("Surface", r"$z = 2- x^2 -y^2$", color = PINK).scale(0.6).move_to(np.array([3*LEFT +3*UP]))
        surface_eqn[0].set_color(BLUE_C)

        line = Line(np.array([-2,0,0]), np.array([2,0,0]), color = RED_C)
        
 
        self.add(axes)

        axis = TextMobject(r"X",r"Y",r"Z")
        axis[0].move_to(6*RIGHT)
        axis[1].move_to(6*UP)
        axis[2].move_to(3.7*UP)

        self.add_fixed_in_frame_mobjects(axis[2])
        self.add_fixed_orientation_mobjects(axis[0])
        self.add_fixed_orientation_mobjects(axis[1])


        self.set_camera_orientation(phi=80 * DEGREES, theta = 0*DEGREES)

        self.play(Write(paraboloid))

        self.add_fixed_in_frame_mobjects(surface_eqn)
        #self.move_camera(phi=80* DEGREES,theta=95*DEGREES)
        self.move_camera(phi=80* DEGREES,theta=45*DEGREES)
        self.play(ShowCreation(plane))
        self.add_fixed_in_frame_mobjects(plane_text)
        self.wait()
        self.play(ReplacementTransform(paraboloid, paraboloid_x))
        self.play(FadeOut(plane), FadeOut(plane_text))
        self.play(ShowCreation(parabola), ShowCreation(line))

        text1 = TextMobject("Moving small", r"$dx$", r"steps").scale(0.6).move_to(3*UP + 3.5*RIGHT).set_color_by_gradient(RED, ORANGE, YELLOW, BLUE, PURPLE)
    
        text2 = TextMobject("Observing change in function, keeping", r"$y$", r"constant").scale(0.6).move_to(2.6*UP + 3.5*RIGHT).set_color_by_gradient(RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE)
        
        slope_text = TexMobject("Slope =", "{\\partial", "f", "\\over", "\\partial", "x}").scale(0.6).move_to(2*UP + 3.5*RIGHT)
        slope_text[0].set_color(BLUE_E)
        slope_text.set_color_by_tex("\\partial",PINK)
        slope_text.set_color_by_tex("f","#006400")
        slope_text[5].set_color(RED_C)

        self.add_fixed_in_frame_mobjects(text1, text2)
        self.wait()
        self.add_fixed_in_frame_mobjects(slope_text)
        #add_fixed_orientation_mobjects
        
        
        dot = Dot().rotate(PI/2).set_color(RED_C)
        alpha = ValueTracker(0)
        vector = self.get_tangent_vector(alpha.get_value(),parabola,scale=1.5)
        dot.add_updater(lambda m: m.move_to(vector.get_center()))
        self.play(
            ShowCreation(parabola),
            GrowFromCenter(dot),
            GrowArrow(vector)
            )
        vector.add_updater(
            lambda m: m.become(
                    self.get_tangent_vector(alpha.get_value()%1,parabola,scale=1.5)
                )
            )
        self.add(vector,dot)
        self.play(alpha.increment_value, 1, run_time=10, rate_func=linear)
        self.wait()


        '''
        for i in np.arange(-2,2,0.2):
        	self.play(ReplacementTransform(Line(np.array([i,0,0]), np.array([i,0,-i*i + 2]), color = GREEN_C), Line(np.array([i+0.2,0,0]), np.array([i+0.2,0,-(i+0.2)**2 + 2]), color = GREEN_C)))
            #self.wait()
        '''

        self.wait()
        self.play(FadeOut(parabola), FadeOut(line), FadeOut(vector), FadeOut(dot), FadeOut(text1), FadeOut(text2), FadeOut(slope_text),FadeOut(surface_eqn))

        #self.move_camera(phi=80* DEGREES,theta= 0*DEGREES)
        self.play(ReplacementTransform(paraboloid_x, paraboloid_copy))
        self.wait()


    def get_tangent_vector(self, proportion, curve, dx=0.001, scale=1):
        coord_i = curve.point_from_proportion(proportion)
        coord_f = curve.point_from_proportion(proportion + dx)
        reference_line = Line(coord_i,coord_f)
        unit_vector = reference_line.get_unit_vector() * scale
        vector = Line(coord_i - unit_vector, coord_i + unit_vector, color = BLUE_E, buff=0)
        return vector


class PartialDerivY(ThreeDScene):
    def construct(self):
        axes = ThreeDAxes() 

        paraboloid = ParametricSurface(
            lambda u, v: np.array([
                2*np.sin(u)*np.cos(v),
                2*np.sin(u)*np.sin(v),
                -2*2*np.sin(u)*np.sin(u)+2
            ]),u_min=0,u_max=PI/2,v_min=0,v_max=2*PI,checkerboard_colors=[PINK, PURPLE],
            resolution=(15, 32)).scale(1)

        paraboloid_copy = paraboloid.copy()

 
        paraboloid_y = ParametricSurface(
            lambda u, v: np.array([
                2*np.sin(u)*np.cos(v),
                2*np.sin(u)*np.sin(v),
                -2*2*np.sin(u)*np.sin(u)+2
            ]),u_min=0,u_max=PI/2,v_min=PI/2,v_max=3*PI/2,checkerboard_colors=[PINK, PURPLE],
            resolution=(15, 32)).scale(1)


        parabola =ParametricFunction(
                lambda u : np.array([
                0,
                u,
                -(u*u) + 2
            ]),color=YELLOW_C,t_min=-2,t_max=2,
            )
        
        plane = Polygon(np.array([0,-2.2,-2.5]),np.array([0,2.2,-2.5]),np.array([0,2.2,2.5]),np.array([0,-2.2,2.5]),np.array([0,-2.2,-2.5]), color = BLUE, fill_color = BLUE, fill_opacity = 0.2)
        plane_text = TextMobject(r"$x = 0$", color = BLUE_C).move_to(2*UP + 3*RIGHT)

        surface_eqn = TextMobject("Surface", r"$z = 2- x^2 -y^2$", color = PINK).scale(0.6).move_to(np.array([3*LEFT +3*UP]))
        surface_eqn[0].set_color(BLUE_C)
        
        line = Line(np.array([0,-2,0]), np.array([0,2,0]), color = RED_C)
 
        self.add(axes)

        axis = TextMobject(r"X",r"Y",r"Z")
        axis[0].move_to(6*RIGHT)
        axis[1].move_to(6*UP)
        axis[2].move_to(3.7*UP)

        self.add_fixed_in_frame_mobjects(axis[2])
        self.add_fixed_orientation_mobjects(axis[0])
        self.add_fixed_orientation_mobjects(axis[1])

        self.set_camera_orientation(phi=80 * DEGREES, theta = 45*DEGREES)

        self.play(Write(paraboloid))

        self.add_fixed_in_frame_mobjects(surface_eqn)
        #self.move_camera(phi=80* DEGREES,theta=5*DEGREES)
        self.play(ShowCreation(plane))
        self.add_fixed_in_frame_mobjects(plane_text)
        self.wait()
        self.play(ReplacementTransform(paraboloid, paraboloid_y))
        self.play(FadeOut(plane), FadeOut(plane_text))
        self.play(ShowCreation(parabola), ShowCreation(line))

        text1 = TextMobject("Moving small", r"$dy$", r"steps").scale(0.6).move_to(3*UP + 3.5*RIGHT).set_color_by_gradient(RED, ORANGE, YELLOW, BLUE, PURPLE)
        
        text2 = TextMobject("Observing change in function, keeping", r"$x$", r"constant").scale(0.6).move_to(2.6*UP + 3.5*RIGHT).set_color_by_gradient(RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE)
        
        slope_text = TexMobject("Slope =", "{\\partial", "f", "\\over", "\\partial", "y}").scale(0.6).move_to(2*UP + 3.5*RIGHT)
        slope_text[0].set_color("#006400")
        slope_text.set_color_by_tex("\\partial",PINK)
        slope_text.set_color_by_tex("f",YELLOW_C)
        slope_text[5].set_color(RED_C)

        self.add_fixed_in_frame_mobjects(text1, text2)
        self.wait()
        self.add_fixed_in_frame_mobjects(slope_text)

        dot = Dot().rotate(PI/2).set_color(RED_C)
        alpha = ValueTracker(0)
        vector = self.get_tangent_vector(alpha.get_value(),parabola,scale=1.5)
        dot.add_updater(lambda m: m.move_to(vector.get_center()))
        self.play(
            ShowCreation(parabola),
            GrowFromCenter(dot),
            GrowArrow(vector)
            )
        vector.add_updater(
            lambda m: m.become(
                    self.get_tangent_vector(alpha.get_value()%1,parabola,scale=1.5)
                )
            )
        self.add(vector,dot)
        self.play(alpha.increment_value, 1, run_time=10, rate_func=linear)
        self.wait()
        
        '''
        for i in np.arange(-2,2,0.2):
        	self.play(ReplacementTransform(Line(np.array([0,i,0]), np.array([0,i,-i*i + 2]), color = BLUE_C), Line(np.array([0,i+0.2,0]), np.array([0,i+0.2,-(i+0.2)**2 + 2]), color = BLUE_C)))
            #self.wait()
        '''


        self.wait()
        self.play(FadeOut(parabola), FadeOut(line), FadeOut(vector), FadeOut(dot), FadeOut(text1), FadeOut(text2), FadeOut(slope_text),FadeOut(surface_eqn))

        #self.move_camera(phi=80* DEGREES,theta= 90*DEGREES)
        self.play(ReplacementTransform(paraboloid_y, paraboloid_copy))
        self.wait()   

    def get_tangent_vector(self, proportion, curve, dx=0.001, scale=1):
        coord_i = curve.point_from_proportion(proportion)
        coord_f = curve.point_from_proportion(proportion + dx)
        reference_line = Line(coord_i,coord_f)
        unit_vector = reference_line.get_unit_vector() * scale
        vector = Line(coord_i - unit_vector, coord_i + unit_vector, color = "#006400", buff=0)
        return vector