Created
June 8, 2022 11:40
-
-
Save alaztetik/5f94e24533c22414a4e8c0423c3df9b8 to your computer and use it in GitHub Desktop.
Newton–Raphson root finding method
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
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
z := float64(1) | |
s := float64(0) | |
for { | |
z -= (z*z - x) / (2*z) | |
if math.Abs(s-z) < 1e-15 { | |
break | |
} | |
s = z | |
} | |
return s | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(math.Sqrt(2)) | |
} | |
// ref_1: A Tour of Go | |
// https://www.wikiwand.com/en/Newton%27s_method | |
// ref_2: Newton's method | |
// https://www.wikiwand.com/en/Newton%27s_method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment