Skip to content

Instantly share code, notes, and snippets.

@uwezi
Last active June 23, 2024 18:54
Show Gist options
  • Save uwezi/d4b1f43706afdf4b05a354dff25ab5b1 to your computer and use it in GitHub Desktop.
Save uwezi/d4b1f43706afdf4b05a354dff25ab5b1 to your computer and use it in GitHub Desktop.
[Intersection points] Find the roots of functions. #manim #root #intersection #function #math #scipy
# https://discord.com/channels/581738731934056449/1216636172277776435/1216684543042650142
from scipy.optimize import root_scalar
# uwezi intersection method
class question(Scene):
def construct(self):
t_dn = DecimalNumber(-5).to_edge(UR)
# x^3 - 4x
def f(x):
return (x-2) * x * (x+2)
def t(x):
return -0.1*x**2 + t_dn.get_value()
axes = Axes()
fplot = axes.plot(f, color = RED)
tplot = always_redraw(
lambda : axes.plot(t, color = WHITE)
)
dots = VGroup()
def dotUpdater(mobj):
roots = []
for x0 in np.arange(-4, 4, 0.1):
root = root_scalar(lambda x: t(x)-f(x), x0 = x0, method = 'newton')
if root.converged:
roots.append(root.root)
roots = np.unique(np.round(roots,2))
grp = VGroup(
*[Dot().set_color(YELLOW).move_to(axes.c2p(root, f(root))) for root in roots]
)
mobj.become(grp)
dots.add_updater(dotUpdater)
self.add(axes, fplot)
self.add(tplot, dots)
self.wait(2)
self.play(ChangeDecimalToValue(t_dn, 4), run_time = 6, rate_func = linear)
self.wait(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment