Last active
April 5, 2021 13:46
-
-
Save Yuuki77/b096e696566bd4792d5d9fbd95f740e7 to your computer and use it in GitHub Desktop.
Bisection Method Sample
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 main() { | |
a := 1.0 | |
b := 2.0 | |
c := 1.5 | |
for math.Abs(calc(c)) > 0.00001 { | |
c = (a + b) / 2.0 | |
fa := calc(a) | |
// fb := calc(b) | |
fc := calc(c) | |
// 符号チェック | |
if fa*fc > 0 { | |
a = c | |
} else { | |
b = c | |
} | |
} | |
fmt.Println(c) | |
fmt.Println(calc(c)) | |
} | |
func calc(x float64) float64 { | |
return x*x - 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment