bracket_solve.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Bracket and Solve Root</title>
  5. <link rel="stylesheet" href="../../math.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../../index.html" title="Math Toolkit 2.11.0">
  8. <link rel="up" href="../roots_noderiv.html" title="Root Finding Without Derivatives">
  9. <link rel="prev" href="bisect.html" title="Bisection">
  10. <link rel="next" href="TOMS748.html" title="Algorithm TOMS 748: Alefeld, Potra and Shi: Enclosing zeros of continuous functions">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="bisect.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../roots_noderiv.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="TOMS748.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h3 class="title">
  27. <a name="math_toolkit.roots_noderiv.bracket_solve"></a><a class="link" href="bracket_solve.html" title="Bracket and Solve Root">Bracket and
  28. Solve Root</a>
  29. </h3></div></div></div>
  30. <pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Tol</span><span class="special">&gt;</span>
  31. <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&gt;</span>
  32. <span class="identifier">bracket_and_solve_root</span><span class="special">(</span>
  33. <span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span>
  34. <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">guess</span><span class="special">,</span>
  35. <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">factor</span><span class="special">,</span>
  36. <span class="keyword">bool</span> <span class="identifier">rising</span><span class="special">,</span>
  37. <span class="identifier">Tol</span> <span class="identifier">tol</span><span class="special">,</span>
  38. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">uintmax_t</span><span class="special">&amp;</span> <span class="identifier">max_iter</span><span class="special">);</span>
  39. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Tol</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
  40. <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&gt;</span>
  41. <span class="identifier">bracket_and_solve_root</span><span class="special">(</span>
  42. <span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span>
  43. <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">guess</span><span class="special">,</span>
  44. <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">factor</span><span class="special">,</span>
  45. <span class="keyword">bool</span> <span class="identifier">rising</span><span class="special">,</span>
  46. <span class="identifier">Tol</span> <span class="identifier">tol</span><span class="special">,</span>
  47. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">uintmax_t</span><span class="special">&amp;</span> <span class="identifier">max_iter</span><span class="special">,</span>
  48. <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
  49. </pre>
  50. <p>
  51. <code class="computeroutput"><span class="identifier">bracket_and_solve_root</span></code> is
  52. a convenience function that calls <a class="link" href="TOMS748.html" title="Algorithm TOMS 748: Alefeld, Potra and Shi: Enclosing zeros of continuous functions">TOMS
  53. 748 algorithm</a> internally to find the root of <span class="emphasis"><em>f(x)</em></span>.
  54. It is generally much easier to use this function rather than <a class="link" href="TOMS748.html" title="Algorithm TOMS 748: Alefeld, Potra and Shi: Enclosing zeros of continuous functions">TOMS
  55. 748 algorithm</a>, since it does the hard work of bracketing the root
  56. for you. It's bracketing routines are quite robust and will usually be more
  57. foolproof than home-grown routines, unless the function can be analysed to
  58. yield tight brackets.
  59. </p>
  60. <p>
  61. Note that this routine can only be used when:
  62. </p>
  63. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  64. <li class="listitem">
  65. <span class="emphasis"><em>f(x)</em></span> is monotonic in the half of the real axis containing
  66. <span class="emphasis"><em>guess</em></span>.
  67. </li>
  68. <li class="listitem">
  69. The value of the inital guess must have the same sign as the root: the
  70. function will <span class="emphasis"><em>never cross the origin</em></span> when searching
  71. for the root.
  72. </li>
  73. <li class="listitem">
  74. The location of the root should be known at least approximately, if the
  75. location of the root differs by many orders of magnitude from <span class="emphasis"><em>guess</em></span>
  76. then many iterations will be needed to bracket the root in spite of the
  77. special heuristics used to guard against this very situation. A typical
  78. example would be setting the initial guess to 0.1, when the root is at
  79. 1e-300.
  80. </li>
  81. </ul></div>
  82. <p>
  83. The <code class="computeroutput"><span class="identifier">bracket_and_solve_root</span></code>
  84. parameters are:
  85. </p>
  86. <div class="variablelist">
  87. <p class="title"><b></b></p>
  88. <dl class="variablelist">
  89. <dt><span class="term">f</span></dt>
  90. <dd><p>
  91. A unary functor (or C++ lambda) that is the function whose root is
  92. to be solved. <span class="emphasis"><em>f(x)</em></span> must be uniformly increasing
  93. or decreasing on <span class="emphasis"><em>x</em></span>.
  94. </p></dd>
  95. <dt><span class="term">guess</span></dt>
  96. <dd><p>
  97. An initial approximation to the root.
  98. </p></dd>
  99. <dt><span class="term">factor</span></dt>
  100. <dd><p>
  101. A scaling factor that is used to bracket the root: the value <span class="emphasis"><em>guess</em></span>
  102. is multiplied (or divided as appropriate) by <span class="emphasis"><em>factor</em></span>
  103. until two values are found that bracket the root. A value such as 2
  104. is a typical choice for <span class="emphasis"><em>factor</em></span>. In addition <span class="emphasis"><em>factor</em></span>
  105. will be multiplied by 2 every 32 iterations: this is to guard against
  106. a really very bad initial guess, typically these occur when it's known
  107. the result is very large or small, but not the exact order of magnitude.
  108. </p></dd>
  109. <dt><span class="term">rising</span></dt>
  110. <dd><p>
  111. Set to <span class="emphasis"><em>true</em></span> if <span class="emphasis"><em>f(x)</em></span> is rising
  112. on <span class="emphasis"><em>x</em></span> and <span class="emphasis"><em>false</em></span> if <span class="emphasis"><em>f(x)</em></span>
  113. is falling on <span class="emphasis"><em>x</em></span>. This value is used along with
  114. the result of <span class="emphasis"><em>f(guess)</em></span> to determine if <span class="emphasis"><em>guess</em></span>
  115. is above or below the root.
  116. </p></dd>
  117. <dt><span class="term">tol</span></dt>
  118. <dd><p>
  119. A binary functor (or C++ lambda) that determines the termination condition
  120. for the search for the root. <span class="emphasis"><em>tol</em></span> is passed the
  121. current brackets at each step, when it returns true then the current
  122. brackets are returned as the pair result. See also <a class="link" href="root_termination.html" title="Termination Condition Functors">predefined
  123. termination functors</a>.
  124. </p></dd>
  125. <dt><span class="term">max_iter</span></dt>
  126. <dd><p>
  127. The maximum number of function invocations to perform in the search
  128. for the root. On exit is set to the actual number of invocations performed.
  129. </p></dd>
  130. </dl>
  131. </div>
  132. <p>
  133. The final <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can
  134. be used to control the behaviour of the function: how it handles errors,
  135. what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">policy
  136. documentation for more details</a>.
  137. </p>
  138. <p>
  139. <span class="bold"><strong>Returns</strong></span>: a pair of values <span class="emphasis"><em>r</em></span>
  140. that bracket the root so that:
  141. </p>
  142. <div class="blockquote"><blockquote class="blockquote"><p>
  143. f(r.first) * f(r.second) &lt;= 0
  144. </p></blockquote></div>
  145. <p>
  146. and either
  147. </p>
  148. <div class="blockquote"><blockquote class="blockquote"><p>
  149. tol(r.first, r.second) == true
  150. </p></blockquote></div>
  151. <p>
  152. or
  153. </p>
  154. <div class="blockquote"><blockquote class="blockquote"><p>
  155. max_iter &gt;= m
  156. </p></blockquote></div>
  157. <p>
  158. where <span class="emphasis"><em>m</em></span> is the initial value of <span class="emphasis"><em>max_iter</em></span>
  159. passed to the function.
  160. </p>
  161. <p>
  162. In other words, it's up to the caller to verify whether termination occurred
  163. as a result of exceeding <span class="emphasis"><em>max_iter</em></span> function invocations
  164. (easily done by checking the value of <span class="emphasis"><em>max_iter</em></span> when
  165. the function returns), rather than because the termination condition <span class="emphasis"><em>tol</em></span>
  166. was satisfied.
  167. </p>
  168. </div>
  169. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  170. <td align="left"></td>
  171. <td align="right"><div class="copyright-footer">Copyright &#169; 2006-2019 Nikhar
  172. Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
  173. Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
  174. R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
  175. Daryle Walker and Xiaogang Zhang<p>
  176. Distributed under the Boost Software License, Version 1.0. (See accompanying
  177. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  178. </p>
  179. </div></td>
  180. </tr></table>
  181. <hr>
  182. <div class="spirit-nav">
  183. <a accesskey="p" href="bisect.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../roots_noderiv.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="TOMS748.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  184. </div>
  185. </body>
  186. </html>