|
| 1 | +# Basic Zero-Finding Utilities |
| 2 | + |
| 3 | +## Overview |
| 4 | +The `basicZeroFun.hpp` file provides implementations of numerical methods for finding zeros of scalar functions. These methods are designed for continuous functions and include classical root-finding algorithms such as: |
| 5 | + |
| 6 | +- Regula Falsi |
| 7 | +- Bisection |
| 8 | +- Secant |
| 9 | +- Newton |
| 10 | +- Brent's method |
| 11 | + |
| 12 | +The file also includes a utility for bracketing intervals that contain zeros. Modern C++ features, such as concepts, are used to ensure type safety and flexibility. |
| 13 | + |
| 14 | +## Key Functions |
| 15 | + |
| 16 | +### 1. `regulaFalsi` |
| 17 | +This function implements the Regula Falsi method, a bracketing method for finding zeros of a function. |
| 18 | + |
| 19 | +#### Template Parameters: |
| 20 | +- `Function`: A callable object with the signature `double(double)` or `double(const double&)`. |
| 21 | + |
| 22 | +#### Parameters: |
| 23 | +- `f`: The function to find the zero of. |
| 24 | +- `a`, `b`: The endpoints of the initial interval. |
| 25 | +- `tol`: Relative tolerance. |
| 26 | +- `tola`: Absolute tolerance. |
| 27 | + |
| 28 | +#### Returns: |
| 29 | +- The approximated zero of the function. |
| 30 | + |
| 31 | +#### Example: |
| 32 | +```cpp |
| 33 | +double root = regulaFalsi([](double x) { return x * x - 2; }, 0.0, 2.0); |
| 34 | +``` |
| 35 | +
|
| 36 | +### 2. `bisection` |
| 37 | +This function implements the Bisection method, a robust bracketing method for finding zeros. |
| 38 | +
|
| 39 | +#### Parameters: |
| 40 | +- `f`: The function to find the zero of. |
| 41 | +- `a`, `b`: The endpoints of the initial interval. |
| 42 | +- `tol`: Tolerance for the result. |
| 43 | +
|
| 44 | +#### Returns: |
| 45 | +- The approximated zero of the function. |
| 46 | +
|
| 47 | +#### Example: |
| 48 | +```cpp |
| 49 | +double root = bisection([](double x) { return x * x - 2; }, 0.0, 2.0); |
| 50 | +``` |
| 51 | + |
| 52 | +### 3. `secant` |
| 53 | +This function implements the Secant method, which uses linear interpolation to find zeros. |
| 54 | + |
| 55 | +#### Parameters: |
| 56 | +- `f`: The function to find the zero of. |
| 57 | +- `a`, `b`: Initial points for the method. |
| 58 | +- `tol`: Relative tolerance. |
| 59 | +- `tola`: Absolute tolerance. |
| 60 | +- `maxIt`: Maximum number of iterations. |
| 61 | + |
| 62 | +#### Returns: |
| 63 | +- A tuple containing the approximated zero and a boolean indicating convergence. |
| 64 | + |
| 65 | +#### Example: |
| 66 | +```cpp |
| 67 | +auto [root, converged] = secant([](double x) { return x * x - 2; }, 0.0, 2.0); |
| 68 | +``` |
| 69 | +
|
| 70 | +### 4. `Newton` |
| 71 | +This function implements Newton's method, which uses the derivative of the function to find zeros. |
| 72 | +
|
| 73 | +#### Parameters: |
| 74 | +- `f`: The function to find the zero of. |
| 75 | +- `df`: The derivative of the function. |
| 76 | +- `a`: Initial guess for the zero. |
| 77 | +- `tol`: Relative tolerance. |
| 78 | +- `tola`: Absolute tolerance. |
| 79 | +- `maxIt`: Maximum number of iterations. |
| 80 | +
|
| 81 | +#### Returns: |
| 82 | +- A tuple containing the approximated zero and a boolean indicating convergence. |
| 83 | +
|
| 84 | +#### Example: |
| 85 | +```cpp |
| 86 | +auto [root, converged] = Newton([](double x) { return x * x - 2; }, [](double x) { return 2 * x; }, 1.0); |
| 87 | +``` |
| 88 | + |
| 89 | +### 5. `bracketInterval` |
| 90 | +This function attempts to find an interval that brackets a zero of a function. |
| 91 | + |
| 92 | +#### Parameters: |
| 93 | +- `f`: The function to analyze. |
| 94 | +- `x1`: Initial point. |
| 95 | +- `h`: Initial increment for sampling. |
| 96 | +- `maxIter`: Maximum number of iterations. |
| 97 | + |
| 98 | +#### Returns: |
| 99 | +- A tuple containing the bracketing points and a boolean indicating success. |
| 100 | + |
| 101 | +#### Example: |
| 102 | +```cpp |
| 103 | +auto [x1, x2, success] = bracketInterval([](double x) { return x * x - 2; }, 1.0); |
| 104 | +``` |
| 105 | +
|
| 106 | +### 6. `brent_search` |
| 107 | +This function implements Brent's method, a combination of bisection, secant, and inverse quadratic interpolation. |
| 108 | +
|
| 109 | +#### Parameters: |
| 110 | +- `f`: The function to find the zero of. |
| 111 | +- `a`, `b`: The endpoints of the bracketing interval. |
| 112 | +- `tol`: Tolerance for the result. |
| 113 | +- `maxIter`: Maximum number of iterations. |
| 114 | +
|
| 115 | +#### Returns: |
| 116 | +- A tuple containing the approximated zero and a boolean indicating convergence. |
| 117 | +
|
| 118 | +#### Example: |
| 119 | +```cpp |
| 120 | +auto [root, converged] = brent_search([](double x) { return x * x - 2; }, 0.0, 2.0); |
| 121 | +``` |
| 122 | + |
| 123 | +## C++ Features Used |
| 124 | + |
| 125 | +### 1. **Concepts** |
| 126 | +The file uses concepts to constrain the template parameters for functions. The `TypeTraits::ScalarFunction` concept ensures that the provided callable objects have the correct signature. |
| 127 | + |
| 128 | +### 2. **Modern C++ Practices** |
| 129 | +- Use of `std::tuple` for returning multiple values. |
| 130 | +- Use of `std::swap` for efficient variable swapping. |
| 131 | +- Assertions (`SURE_ASSERT`) to enforce preconditions. |
| 132 | + |
| 133 | +## Usage Notes |
| 134 | + |
| 135 | +1. **Function Requirements**: |
| 136 | + - The functions must be continuous. |
| 137 | + - For Newton's method, the derivative must be provided. |
| 138 | + |
| 139 | +2. **Convergence**: |
| 140 | + - The bracketing methods (`regulaFalsi`, `bisection`, `brent_search`) guarantee convergence if the initial interval brackets a zero. |
| 141 | + - The open methods (`secant`, `Newton`) may fail to converge if the initial guesses are poor. |
| 142 | + |
| 143 | +3. **Performance**: |
| 144 | + - Brent's method is generally efficient and robust. |
| 145 | + - Bisection is the most reliable (if the function is continuous and brackets a zero) but slower. |
| 146 | + - Newton's method can be very fast (quadratic convergence for C^2 functions) but requires a good initial guess and the derivative. |
| 147 | + |
| 148 | +## Conclusion |
| 149 | +The `basicZeroFun.hpp` file provides a comprehensive set of tools for zero-finding of univariate functions, leveraging modern C++ features for robustness and flexibility. These utilities are suitable for a wide range of applications requiring root-finding algorithms. |
0 commit comments