Skip to content

Instantly share code, notes, and snippets.

@pgk
Created December 1, 2017 14:22
Show Gist options
  • Save pgk/dda250df4ee268c8e88acb6c687de18f to your computer and use it in GitHub Desktop.
Save pgk/dda250df4ee268c8e88acb6c687de18f to your computer and use it in GitHub Desktop.
# From calculation, it is expected that the local minimum occurs at x=9/4
cur_x = 6 # The algorithm starts at x=6
gamma = 0.01 # step size multiplier
precision = 0.00001
previous_step_size = cur_x
df = lambda x: 4 * x**3 - 9 * x**2
while previous_step_size > precision:
prev_x = cur_x
cur_x += -gamma * df(prev_x)
previous_step_size = abs(cur_x - prev_x)
print("The local minimum occurs at %f" % cur_x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment