0% found this document useful (0 votes)
9 views2 pages

Chapt GPT

The document contains a Manim script that visualizes a minimization algorithm for the function f(x) = x^2 - 4x + 4. It demonstrates the process of finding the minimum point of the function using an iterative approach, displaying the function graph, initial points, and the optimal point. The visualization includes animations for the function, points, and labels indicating the optimal value and point.

Uploaded by

Aansh Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Chapt GPT

The document contains a Manim script that visualizes a minimization algorithm for the function f(x) = x^2 - 4x + 4. It demonstrates the process of finding the minimum point of the function using an iterative approach, displaying the function graph, initial points, and the optimal point. The visualization includes animations for the function, points, and labels indicating the optimal value and point.

Uploaded by

Aansh Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

%%manim -qm -v WARNING MinimizationGraph

from manim import *

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 interval [a, b]


a, b = 3, 5
epsilon = 1e-6
iterations = 0

# 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)

# Minimization algorithm visualization (fewer iterations for speed)


while abs(b - a) > epsilon and iterations < 15:
fa, fb = f(a), f(b)
ga, gb = f_dash(a), f_dash(b)

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))

# Ensure val stays inside [a, b]


val = max(a, min(b, val))
fVal, f_dashVal = f(val), f_dash(val)

# Create new point


dot_val = Dot(point=axes.c2p(val, fVal), color=YELLOW)
self.play(FadeIn(dot_val), run_time=0.2)
# Update interval
if ga < 0 and (f_dashVal > 0 or fVal > fa):
b = val
else:
a = val

# 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

# Final minimum point


min_x, min_y = a, f(a)
min_dot = Dot(point=axes.c2p(min_x, min_y), color=GREEN, radius=0.15)
min_label = MathTex(f"\\text{{Optimal Point: }} x =
{min_x:.2f}").next_to(min_dot, UP)
min_value_label = MathTex(f"\\text{{Optimal Value: }} f(x) =
{min_y:.2f}").next_to(min_label, DOWN)

self.play(FadeIn(min_dot), Write(min_label), Write(min_value_label),


run_time=0.5)
self.play(Indicate(min_dot, color=GREEN), run_time=0.5)
self.wait(1)

You might also like