You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1010 B

3 months ago
from manim import *
class ArgMinExample(Scene):
def construct(self):
ax = Axes(
x_range=[
0, 10], y_range=[
0, 100, 10], axis_config={
"include_tip": False})
labels = ax.get_axis_labels(x_label="x", y_label="f(x)")
t = ValueTracker(0)
def func(x):
return 2 * (x - 5) ** 2
graph = ax.plot(func, color=MAROON)
initial_point = [
ax.coords_to_point(
t.get_value(), func(
t.get_value()))]
dot = Dot(point=initial_point)
dot.add_updater(
lambda x: x.move_to(
ax.c2p(
t.get_value(),
func(
t.get_value()))))
x_space = np.linspace(*ax.x_range[:2], 200)
minimum_index = func(x_space).argmin()
self.add(ax, labels, graph, dot)
self.play(t.animate.set_value(x_space[minimum_index]))
self.wait()