Skip to content

Instantly share code, notes, and snippets.

@zhhailon
Created November 14, 2011 09:51
Show Gist options
  • Save zhhailon/1363631 to your computer and use it in GitHub Desktop.
Save zhhailon/1363631 to your computer and use it in GitHub Desktop.
Newton Iteration Method
#!/usr/bin/env ruby
# Created by ZHANG Hailong <[email protected]>
# Newton Iteration Method
# f(x) = X^2 - a = 0. Here a = 2.
# Xn+1 = Xn - (Xn^2 - 2) / 2Xn = Xn / 2 + 1 / Xn.
x0 = 1.0
n = 5
x = x0 / 2 + 1 / x0
(n - 1).times do
x = x / 2 + 1 / x
end
puts x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment