findroot_demo.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Boost example/findroot_demo.cpp
  2. * find zero points of some function by dichotomy
  3. *
  4. * Copyright 2000 Jens Maurer
  5. * Copyright 2002-2003 Guillaume Melquiond
  6. *
  7. * Distributed under the Boost Software License, Version 1.0.
  8. * (See accompanying file LICENSE_1_0.txt or
  9. * copy at http://www.boost.org/LICENSE_1_0.txt)
  10. *
  11. * The idea and the 2D function are based on RVInterval,
  12. * which contains the following copyright notice:
  13. This file is copyrighted 1996 by Ronald Van Iwaarden.
  14. Permission is hereby granted, without written agreement and
  15. without license or royalty fees, to use, copy, modify, and
  16. distribute this software and its documentation for any
  17. purpose, subject to the following conditions:
  18. The above license notice and this permission notice shall
  19. appear in all copies or substantial portions of this software.
  20. The name "RVInterval" cannot be used for any modified form of
  21. this software that does not originate from the authors.
  22. Nevertheless, the name "RVInterval" may and should be used to
  23. designate the optimization software implemented and described
  24. in this package, even if embedded in any other system, as long
  25. as any portion of this code remains.
  26. The authors specifically disclaim any warranties, including,
  27. but not limited to, the implied warranties of merchantability
  28. and fitness for a particular purpose. The software provided
  29. hereunder is on an "as is" basis, and the authors have no
  30. obligation to provide maintenance, support, updates,
  31. enhancements, or modifications. In no event shall the authors
  32. be liable to any party for direct, indirect, special,
  33. incidental, or consequential damages arising out of the use of
  34. this software and its documentation.
  35. */
  36. #include <boost/numeric/interval.hpp> // must be first for <limits> workaround
  37. #include <boost/numeric/interval/io.hpp>
  38. #include <list>
  39. #include <deque>
  40. #include <vector>
  41. #include <fstream>
  42. #include <iostream>
  43. template<class T>
  44. struct test_func2d
  45. {
  46. T operator()(T x, T y) const
  47. {
  48. return sin(x)*cos(y) - exp(x*y)/45.0 * (pow(x+y, 2)+100.0) -
  49. cos(sin(y))*y/4.0;
  50. }
  51. };
  52. template <class T>
  53. struct test_func1d
  54. {
  55. T operator()(T x) const
  56. {
  57. return sin(x)/(x*x+1.0);
  58. }
  59. };
  60. template<class T>
  61. struct test_func1d_2
  62. {
  63. T operator()(T x) const
  64. {
  65. using std::sqrt;
  66. return sqrt(x*x-1.0);
  67. }
  68. };
  69. template<class Function, class I>
  70. void find_zeros(std::ostream & os, Function f, I searchrange)
  71. {
  72. std::list<I> l, done;
  73. l.push_back(searchrange);
  74. while(!l.empty()) {
  75. I range = l.front();
  76. l.pop_front();
  77. I val = f(range);
  78. if (zero_in(val)) {
  79. if(width(range) < 1e-6) {
  80. os << range << '\n';
  81. continue;
  82. }
  83. // there's still a solution hidden somewhere
  84. std::pair<I,I> p = bisect(range);
  85. l.push_back(p.first);
  86. l.push_back(p.second);
  87. }
  88. }
  89. }
  90. template<class T>
  91. std::ostream &operator<<(std::ostream &os, const std::pair<T, T> &x) {
  92. os << "(" << x.first << ", " << x.second << ")";
  93. return os;
  94. }
  95. template<class T, class Policies>
  96. std::ostream &operator<<(std::ostream &os,
  97. const boost::numeric::interval<T, Policies> &x) {
  98. os << "[" << x.lower() << ", " << x.upper() << "]";
  99. return os;
  100. }
  101. static const double epsilon = 5e-3;
  102. template<class Function, class I>
  103. void find_zeros(std::ostream & os, Function f, I rx, I ry)
  104. {
  105. typedef std::pair<I, I> rectangle;
  106. typedef std::deque<rectangle> container;
  107. container l, done;
  108. // l.reserve(50);
  109. l.push_back(std::make_pair(rx, ry));
  110. for(int i = 1; !l.empty(); ++i) {
  111. rectangle rect = l.front();
  112. l.pop_front();
  113. I val = f(rect.first, rect.second);
  114. if (zero_in(val)) {
  115. if(width(rect.first) < epsilon && width(rect.second) < epsilon) {
  116. os << median(rect.first) << " " << median(rect.second) << " "
  117. << lower(rect.first) << " " << upper(rect.first) << " "
  118. << lower(rect.second) << " " << upper(rect.second)
  119. << '\n';
  120. } else {
  121. if(width(rect.first) > width(rect.second)) {
  122. std::pair<I,I> p = bisect(rect.first);
  123. l.push_back(std::make_pair(p.first, rect.second));
  124. l.push_back(std::make_pair(p.second, rect.second));
  125. } else {
  126. std::pair<I,I> p = bisect(rect.second);
  127. l.push_back(std::make_pair(rect.first, p.first));
  128. l.push_back(std::make_pair(rect.first, p.second));
  129. }
  130. }
  131. }
  132. if(i % 10000 == 0)
  133. std::cerr << "\rIteration " << i << ", l.size() = " << l.size();
  134. }
  135. std::cerr << '\n';
  136. }
  137. int main()
  138. {
  139. using namespace boost;
  140. using namespace numeric;
  141. using namespace interval_lib;
  142. typedef interval<double,
  143. policies<save_state<rounded_transc_opp<double> >,
  144. checking_base<double> > > I;
  145. std::cout << "Zero points of sin(x)/(x*x+1)\n";
  146. find_zeros(std::cout, test_func1d<I>(), I(-11, 10));
  147. std::cout << "Zero points of sqrt(x*x-1)\n";
  148. find_zeros(std::cout, test_func1d_2<I>(), I(-5, 6));
  149. std::cout << "Zero points of Van Iwaarden's 2D function\n";
  150. std::ofstream f("func2d.data");
  151. find_zeros(f, test_func2d<I>(), I(-20, 20), I(-20, 20));
  152. std::cout << "Use gnuplot, command 'plot \"func2d.data\" with dots' to plot\n";
  153. }