Skip to content

Commit

Permalink
Finished a prototype Euler Integration scene
Browse files Browse the repository at this point in the history
  • Loading branch information
sidlim committed Apr 8, 2021
1 parent cc229a6 commit bf01081
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
5 changes: 0 additions & 5 deletions electric_field_simulation/euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ def __init__(self, color = BLUE_E, sign = "-", **kwargs):
super(Electron, self).__init__(color = color, sign = sign, **kwargs)

class ChangingElectricField(Scene):
CONFIG = {
"vector_field_config": {},
"num_particles": 6,
"anim_time": 5
}
def __init__(self, vector_field_config = {}, num_particles = 6, anim_time = 5, **kwargs):
super(ChangingElectricField, self).__init__(**kwargs)
self.vector_field_config = vector_field_config
Expand Down
50 changes: 49 additions & 1 deletion matrix_exp/euler_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,52 @@
# Table of contents:
# 1 - motivation: 4 moving parts to the DE, complexity
# 2 - 1D case
# 3 - 2D case
# 3 - 2D case

class Euler_Integration_2D(Scene):
def __init__(self, derivative, init_val, dt, n_step, **kwargs):
super(Euler_Integration_2D, self).__init__(**kwargs)
self.derivative = derivative
self.init_val = init_val
self.dt = dt
self.n_step = n_step

def construct(self):
vec_field = VectorField(self.derivative)
self.add(vec_field)

x_n = self.init_val

x_pt = Dot()
x_pt.move_to(x_n)
x_pt_label = MathTex("x_" + str(0)).next_to(x_pt, direction = DOWN)
self.play(FadeIn(x_pt), FadeIn(x_pt_label))
self.wait(2)

for n in range(self.n_step):

Dx = numpy.append(self.derivative(x_n), 0.0)
Dx_vec = Vector(Dx).put_start_and_end_on(x_n, x_n + Dx)
Dx_vec_label_pre = MathTex("\\frac{dx}{dt}").next_to(Dx_vec, direction = DOWN)
Dx_vec_label_post = MathTex("\\frac{1}{" + str(int(1 / self.dt)) + "}\\frac{dx}{dt}").next_to(Dx_vec, direction = DOWN)
self.play(FadeIn(Dx_vec), FadeIn(Dx_vec_label_pre))
self.wait(2)
self.play(Dx_vec.animate.scale(self.dt, about_point = Dx_vec.get_start()), ReplacementTransform(Dx_vec_label_pre, Dx_vec_label_post))
#self.play(Dx_vec.animate.put_start_and_end_on(x_n, x_n + self.dt * Dx))

x_pt = Dot()
x_pt.move_to(x_n + self.dt * Dx)
x_pt_label = MathTex("x_" + str(n + 1)).next_to(x_pt, direction = DOWN)
self.play(FadeIn(x_pt))
self.wait(2)

self.play(FadeOut(Dx_vec), FadeOut(Dx_vec_label_post), FadeIn(x_pt_label))

x_n = x_n + self.dt * Dx

class Linear_2D_Int(Euler_Integration_2D):
def __init__(self):
A = numpy.array([[0.0, -1.0], [1.0, 0.0]])
Ax = lambda p: numpy.matmul(A, p[0:2])
x0 = numpy.array([1.5, 2.0, 0.0])
super(Linear_2D_Int, self).__init__(Ax, x0, 0.5, 2)
26 changes: 26 additions & 0 deletions matrix_exp/matrix_exponential.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@
# draw Ax & then show rescale animation by t or t/2
# Show sum as a new point

class Euler_Integration_2D(Scene):
def __init__(self, derivative, init_val, dt, n_step, **kwargs):
super(Euler_Integration_2D, self).__init__(**kwargs)
self.derivative = derivative
self.init_val = init_val
self.dt = dt
self.n_step = n_step

def construct(self):
vec_field = VectorField(self.derivative)
x_n = self.init_val
for n in range(self.n_step):

x_pt = Dot(point = x_n)
x_pt_label = MathTex("x_" + str(n)).next_to(x_pt, direction = DOWN)
self.play(FadeIn(x_pt), FadeIn(x_pt_label))
self.wait(2)

Dx = vec_field.get_vector(x_n)
Dx_vec = Vector(Dx).put_start_and_end_on(x_n, x_n + Dx)
Dx_vec_label = MathTex("\\frac{dx}{dt}").next_to(Dx_vec, direction = DOWN)
self.play(FadeIn(Dx_vec), FadeIn(Dx_vec_label))
self.wait(2)
self.play(Dx_vec.animate.scale(self.dt))



# Second thing to do: Write animation for the algebraic expression
# Highlight elements of the line as we go:
Expand Down

0 comments on commit bf01081

Please sign in to comment.