|
| 1 | +package search |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +// Package search provides functions for searching and root-finding algorithms. |
| 8 | +// |
| 9 | +// bisection searches for the root of the function f in the interval [a, b]. |
| 10 | +// The function f must be continuous and f(a) and f(b) must have different signs. |
| 11 | +// This method uses the bisection algorithm to iteratively narrow down the interval |
| 12 | +// until the root is found within the specified tolerance or the maximum number of |
| 13 | +// iterations is reached. |
| 14 | +// |
| 15 | +// Parameters: |
| 16 | +// - f: A function that takes a float64 and returns a float64. This is the function |
| 17 | +// for which we are trying to find the root. |
| 18 | +// - a: The start of the interval. |
| 19 | +// - b: The end of the interval. |
| 20 | +// - tol: The tolerance for the root. The algorithm stops when the interval width |
| 21 | +// is less than this value. |
| 22 | +// - maxIter: The maximum number of iterations to perform. |
| 23 | +// |
| 24 | +// Returns: |
| 25 | +// - float64: The approximate root of the function. |
| 26 | +// - error: An error is returned if f(a) and f(b) do not have different signs or |
| 27 | +// if the maximum number of iterations is reached without finding the root. |
| 28 | +// |
| 29 | +// Complexity: |
| 30 | +// - Worst: O(log((b-a)/tol)) |
| 31 | +// - Average: O(log((b-a)/tol)) |
| 32 | +// - Best: O(1) |
| 33 | +// |
| 34 | +// Example usage: |
| 35 | +// |
| 36 | +// root, err := bisection(func(x float64) float64 { return math.Pow(x, 3) - x - 2 }, 1, 2, 1e-6, 1000) |
| 37 | +// if err != nil { |
| 38 | +// log.Fatal(err) |
| 39 | +// } |
| 40 | +// fmt.Println(root) // Output: 1.521379 |
| 41 | +// |
| 42 | +// Reference: |
| 43 | +// - https://en.wikipedia.org/wiki/Bisection_method |
| 44 | +func bisection(f func(float64) float64, a, b, tol float64, maxIter int) (float64, error) { |
| 45 | + if f(a)*f(b) >= 0 { |
| 46 | + return 0, fmt.Errorf("f(a) and f(b) must have different signs") |
| 47 | + } |
| 48 | + |
| 49 | + var c float64 |
| 50 | + for i := 0; i < maxIter; i++ { |
| 51 | + c = (a + b) / 2 |
| 52 | + if f(c) == 0 || (b-a)/2 < tol { |
| 53 | + return c, nil |
| 54 | + } |
| 55 | + if f(c)*f(a) < 0 { |
| 56 | + b = c |
| 57 | + } else { |
| 58 | + a = c |
| 59 | + } |
| 60 | + } |
| 61 | + return c, fmt.Errorf("maximum number of iterations reached") |
| 62 | +} |
0 commit comments