gsl_roots__bisection.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* roots/bisection.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /* bisection.c -- bisection root finding algorithm */
  20. #include "gsl__config.h"
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <float.h>
  26. #include "gsl_math.h"
  27. #include "gsl_errno.h"
  28. #include "gsl_roots.h"
  29. #include "gsl_roots__roots.h"
  30. typedef struct
  31. {
  32. double f_lower, f_upper;
  33. }
  34. bisection_state_t;
  35. static int bisection_init (void * vstate, gsl_function * f, double * root, double x_lower, double x_upper);
  36. static int bisection_iterate (void * vstate, gsl_function * f, double * root, double * x_lower, double * x_upper);
  37. static int
  38. bisection_init (void * vstate, gsl_function * f, double * root, double x_lower, double x_upper)
  39. {
  40. bisection_state_t * state = (bisection_state_t *) vstate;
  41. double f_lower, f_upper ;
  42. *root = 0.5 * (x_lower + x_upper) ;
  43. SAFE_FUNC_CALL (f, x_lower, &f_lower);
  44. SAFE_FUNC_CALL (f, x_upper, &f_upper);
  45. state->f_lower = f_lower;
  46. state->f_upper = f_upper;
  47. if ((f_lower < 0.0 && f_upper < 0.0) || (f_lower > 0.0 && f_upper > 0.0))
  48. {
  49. GSL_ERROR ("endpoints do not straddle y=0", GSL_EINVAL);
  50. }
  51. return GSL_SUCCESS;
  52. }
  53. static int
  54. bisection_iterate (void * vstate, gsl_function * f, double * root, double * x_lower, double * x_upper)
  55. {
  56. bisection_state_t * state = (bisection_state_t *) vstate;
  57. double x_bisect, f_bisect;
  58. const double x_left = *x_lower ;
  59. const double x_right = *x_upper ;
  60. const double f_lower = state->f_lower;
  61. const double f_upper = state->f_upper;
  62. if (f_lower == 0.0)
  63. {
  64. *root = x_left ;
  65. *x_upper = x_left;
  66. return GSL_SUCCESS;
  67. }
  68. if (f_upper == 0.0)
  69. {
  70. *root = x_right ;
  71. *x_lower = x_right;
  72. return GSL_SUCCESS;
  73. }
  74. x_bisect = (x_left + x_right) / 2.0;
  75. SAFE_FUNC_CALL (f, x_bisect, &f_bisect);
  76. if (f_bisect == 0.0)
  77. {
  78. *root = x_bisect;
  79. *x_lower = x_bisect;
  80. *x_upper = x_bisect;
  81. return GSL_SUCCESS;
  82. }
  83. /* Discard the half of the interval which doesn't contain the root. */
  84. if ((f_lower > 0.0 && f_bisect < 0.0) || (f_lower < 0.0 && f_bisect > 0.0))
  85. {
  86. *root = 0.5 * (x_left + x_bisect) ;
  87. *x_upper = x_bisect;
  88. state->f_upper = f_bisect;
  89. }
  90. else
  91. {
  92. *root = 0.5 * (x_bisect + x_right) ;
  93. *x_lower = x_bisect;
  94. state->f_lower = f_bisect;
  95. }
  96. return GSL_SUCCESS;
  97. }
  98. static const gsl_root_fsolver_type bisection_type =
  99. {"bisection", /* name */
  100. sizeof (bisection_state_t),
  101. &bisection_init,
  102. &bisection_iterate};
  103. const gsl_root_fsolver_type * gsl_root_fsolver_bisection = &bisection_type;