Chapt GPT
Chapt GPT
class MinimizationGraph(Scene):
def construct(self):
# Define function and derivative
def f(x):
return x * x - 4 * x + 4 # Function to minimize
def f_dash(x):
return 2 * x - 4 # Derivative of f(x)
# Create axes
axes = Axes(
x_range=[0, 6, 1],
y_range=[0, 10, 2],
x_length=8,
y_length=5,
axis_config={"color": GREY},
tips=False
)
axes_labels = axes.get_axis_labels(x_label="x", y_label="f(x)")
# Plot function
graph = axes.plot(f, color=BLUE, x_range=[0, 6])
# Title
title = Text("Minimization Algorithm", font_size=24).to_edge(UP)
# Initial points
dot_a = Dot(point=axes.c2p(a, f(a)), color=RED)
dot_b = Dot(point=axes.c2p(b, f(b)), color=RED)
# Animate setup
self.play(Create(axes), Write(axes_labels), Write(title), run_time=0.5)
self.play(Create(graph), FadeIn(dot_a), FadeIn(dot_b), run_time=0.5)
w = ga + gb - 3 * (fa - fb) / (a - b)
u = max(0.0, w * w - ga * gb) ** 0.5
val = a + (b - a) * (1 - (gb + u - w) / (gb - ga + 2 * u))
# Move points
self.play(dot_a.animate.move_to(axes.c2p(a, f(a))),
dot_b.animate.move_to(axes.c2p(b, f(b))), run_time=0.2)
iterations += 1