Created
May 18, 2016 07:46
-
-
Save wangchauyan/56f2e753ee5bb93edccb8eb8ca7c611b to your computer and use it in GitHub Desktop.
this is an example that shows how to use newton method by go.
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
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func Newt(x float64) float64 { | |
if x == 0 { | |
return 0; | |
} | |
z := 1.0 | |
for i := 0; i < int(x); i++ { | |
z = z - (math.Pow(z, 2) - x) / (2 * z) | |
} | |
return z | |
} | |
func Sqrt(x float64) float64 { | |
return math.Sqrt(x) | |
} | |
func main() { | |
fmt.Println("sqrt value", Sqrt(20)) | |
fmt.Println("sqrt value", Newt(20)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment