MCPcopy Create free account
hub / github.com/tdewolff/canvas / bisectionMethod

Function bisectionMethod

util.go:1158–1183  ·  view source on GitHub ↗

func lookupMin(f func(float64) float64, xmin, xmax float64) float64 { const MaxIterations = 1000 min := math.Inf(1) for i := 0; i <= MaxIterations; i++ { t := float64(i) / float64(MaxIterations) x := xmin + t*(xmax-xmin) y := f(x) if y < min { min = y } } return min } func gradien

(f func(float64) float64, y, xmin, xmax float64)

Source from the content-addressed store, hash-verified

1156
1157// find value x for which f(x) = y in the interval x in [xmin, xmax] using the bisection method
1158func bisectionMethod(f func(float64) float64, y, xmin, xmax float64) float64 {
1159 const MaxIterations = 100
1160 const Tolerance = 0.001 // 0.1%
1161
1162 n := 0
1163 toleranceX := math.Abs(xmax-xmin) * Tolerance
1164 toleranceY := math.Abs(f(xmax)-f(xmin)) * Tolerance
1165
1166 var x float64
1167 for {
1168 x = (xmin + xmax) / 2.0
1169 if n >= MaxIterations {
1170 return x
1171 }
1172
1173 dy := f(x) - y
1174 if math.Abs(dy) < toleranceY || math.Abs(xmax-xmin)/2.0 < toleranceX {
1175 return x
1176 } else if dy > 0.0 {
1177 xmax = x
1178 } else {
1179 xmin = x
1180 }
1181 n++
1182 }
1183}
1184
1185// polynomialApprox returns a function y(x) that maps the parameter x [xmin,xmax] to the integral of fp. For a circle tmin and tmax would be 0 and 2PI respectively for example. It also returns the total length of the curve. Implemented using M. Walter, A. Fournier, Approximate Arc Length Parametrization, Anais do IX SIBGRAPHI, p. 143--150, 1996, see https://www.visgraf.impa.br/sibgrapi96/trabs/pdf/a14.pdf
1186//func polynomialApprox3(gaussLegendre gaussLegendreFunc, fp func(float64) float64, xmin, xmax float64) (func(float64) float64, float64) {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected