Skip to content

Instantly share code, notes, and snippets.

@Yuuki77
Last active April 5, 2021 13:46
Show Gist options
  • Save Yuuki77/b096e696566bd4792d5d9fbd95f740e7 to your computer and use it in GitHub Desktop.
Save Yuuki77/b096e696566bd4792d5d9fbd95f740e7 to your computer and use it in GitHub Desktop.
Bisection Method Sample
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