| 1 | // Copyright (c) 2015, Mikhail Vorotilov |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 18 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 20 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 21 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 22 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | |
| 25 | use super::super::FloatType; |
| 26 | use super::Convergency; |
| 27 | use super::SearchError; |
| 28 | |
| 29 | /// Illinois modification to the classical method |
| 30 | #[derive(Debug, PartialEq)] |
| 31 | enum Edge { |
| 32 | /// Value is close to X1, reduce the Y1 weight |
| 33 | EdgeX1, |
| 34 | /// Value is in the middle of the interval |
| 35 | NoEdge, |
| 36 | /// Value is close to X2, reduce the Y2 weight |
| 37 | EdgeX2, |
| 38 | } |
| 39 | |
| 40 | /// Find a root of the function f(x) = 0 using the Illinois modification of the regula falsi method. |
| 41 | /// |
| 42 | /// Pro |
| 43 | /// |
| 44 | /// + Simple |
| 45 | /// + Robust |
| 46 | /// + No need for derivative function |
| 47 | /// |
| 48 | /// Contra |
| 49 | /// |
| 50 | /// - Slow |
| 51 | /// - Needs initial bracketing |
| 52 | /// |
| 53 | /// # Failures |
| 54 | /// ## NoBracketing |
| 55 | /// Initial values do not bracket the root. |
| 56 | /// ## NoConvergency |
| 57 | /// Algorithm cannot find a root within the given number of iterations. |
| 58 | /// # Examples |
| 59 | /// |
| 60 | /// ``` |
| 61 | /// use roots::SimpleConvergency; |
| 62 | /// use roots::find_root_regula_falsi; |
| 63 | /// |
| 64 | /// let f = |x| { 1f64*x*x - 1f64 }; |
| 65 | /// let mut convergency = SimpleConvergency { eps:1e-15f64, max_iter:30 }; |
| 66 | /// |
| 67 | /// let root1 = find_root_regula_falsi(10f64, 0f64, &f, &mut convergency); |
| 68 | /// // Returns approximately Ok(1); |
| 69 | /// |
| 70 | /// let root2 = find_root_regula_falsi(-10f64, 0f64, &f, &mut 1e-15f64); |
| 71 | /// // Returns approximately Ok(-1); |
| 72 | /// ``` |
| 73 | pub fn find_root_regula_falsi<F, Func>(a: F, b: F, mut f: Func, convergency: &mut dyn Convergency<F>) -> Result<F, SearchError> |
| 74 | where |
| 75 | F: FloatType, |
| 76 | Func: FnMut(F) -> F, |
| 77 | { |
| 78 | let _2 = F::from(2i16); |
| 79 | let (mut x1, mut x2) = if a > b { (b, a) } else { (a, b) }; |
| 80 | let mut y1 = f(x1); |
| 81 | if convergency.is_root_found(y1) { |
| 82 | return Ok(x1); |
| 83 | } |
| 84 | let mut y2 = f(x2); |
| 85 | if convergency.is_root_found(y2) { |
| 86 | return Ok(x2); |
| 87 | } |
| 88 | if y1 * y2 > F::zero() { |
| 89 | return Err(SearchError::NoBracketing); |
| 90 | } |
| 91 | let mut edge = Edge::NoEdge; |
| 92 | let mut iter = 0; |
| 93 | loop { |
| 94 | let x = (x1 * y2 - x2 * y1) / (y2 - y1); |
| 95 | if convergency.is_converged(x1, x2) { |
| 96 | return Ok(x); |
| 97 | } |
| 98 | let y = f(x); |
| 99 | if convergency.is_root_found(y) { |
| 100 | return Ok(x); |
| 101 | } |
| 102 | |
| 103 | if y * y1 > F::zero() { |
| 104 | x1 = x; |
| 105 | y1 = y; |
| 106 | if edge == Edge::EdgeX1 { |
| 107 | y2 = y2 / _2; |
| 108 | } |
| 109 | edge = Edge::EdgeX1; |
| 110 | } else if y * y2 > F::zero() { |
| 111 | x2 = x; |
| 112 | y2 = y; |
| 113 | if edge == Edge::EdgeX2 { |
| 114 | y1 = y1 / _2; |
| 115 | } |
| 116 | edge = Edge::EdgeX2; |
| 117 | } else { |
| 118 | return Ok(x); |
| 119 | } |
| 120 | |
| 121 | iter = iter + 1; |
| 122 | if convergency.is_iteration_limit_reached(iter) { |
| 123 | return Err(SearchError::NoConvergency); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | #[cfg(test)] |
| 129 | mod test { |
| 130 | use super::super::*; |
| 131 | use super::*; |
| 132 | |
| 133 | #[test] |
| 134 | fn test_find_root_regula_falsi() { |
| 135 | let f = |x| 1f64 * x * x - 1f64; |
| 136 | let mut conv = debug_convergency::DebugConvergency::new(1e-15f64, 30); |
| 137 | |
| 138 | conv.reset(); |
| 139 | assert_float_eq!( |
| 140 | 1e-15f64, |
| 141 | find_root_regula_falsi(10f64, 0f64, &f, &mut conv).ok().unwrap(), |
| 142 | 1f64 |
| 143 | ); |
| 144 | assert_eq!(11, conv.get_iter_count()); |
| 145 | |
| 146 | conv.reset(); |
| 147 | assert_float_eq!( |
| 148 | 1e-15f64, |
| 149 | find_root_regula_falsi(-10f64, 0f64, &f, &mut conv).ok().unwrap(), |
| 150 | -1f64 |
| 151 | ); |
| 152 | assert_eq!(11, conv.get_iter_count()); |
| 153 | |
| 154 | conv.reset(); |
| 155 | assert_eq!( |
| 156 | find_root_regula_falsi(10f64, 20f64, &f, &mut conv), |
| 157 | Err(SearchError::NoBracketing) |
| 158 | ); |
| 159 | let result = find_root_regula_falsi(10f64, 20f64, &f, &mut conv); |
| 160 | assert_eq!(result.unwrap_err().to_string(), "Bracketing Error"); |
| 161 | assert_eq!(0, conv.get_iter_count()); |
| 162 | } |
| 163 | } |