Skip to content

Instantly share code, notes, and snippets.

@kumanna
Created April 4, 2025 10:37
Show Gist options
  • Save kumanna/cb9f3dd77742b226bc4224d71d1c71ba to your computer and use it in GitHub Desktop.
Save kumanna/cb9f3dd77742b226bc4224d71d1c71ba to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10, 10, 0.01)
f = lambda x : x*x/4
y = f(x)
x0 = np.abs(np.random.randn() * 5) # initial point
x_vals = []
y_vals = []
DELTA = 0.1
mul_sign = 1
y0 = f(x0)
while abs(y0) > 1e-14:
x_vals.append(x0)
y_vals.append(y0)
new_x0 = x0 - mul_sign * DELTA
new_y0 = f(new_x0)
if new_y0 < y0:
x0 = new_x0
y0 = new_y0
else:
DELTA = DELTA / 2
mul_sign *= -1
print((x0, y0))
#plt.plot(x, y)
#plt.scatter(x_vals, y_vals, c='k')
#plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment