Created
August 19, 2009 17:08
-
-
Save jaydonnell/170468 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# gradient descent in ruby | |
def find_min(x_old, x_new, precision, eps, &block) | |
while (x_new - x_old).abs > precision | |
x_old = x_new | |
tmp = yield x_new | |
x_new = x_new - eps * tmp | |
end | |
x_new | |
end | |
# usage: the block contains the derivative of f(x)=x^4-3x^3+2 | |
# find_min(0, 0.1, 0.00001, 0.01) { |x| (4 * (x**3)) - (9 * (x**2)) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment