This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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