Skip to content

Instantly share code, notes, and snippets.

@jaydonnell
Created August 19, 2009 17:08
Show Gist options
  • Save jaydonnell/170468 to your computer and use it in GitHub Desktop.
Save jaydonnell/170468 to your computer and use it in GitHub Desktop.
# 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