gsl_roots__falsepos.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* roots/falsepos.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. /* falsepos.c -- falsepos root finding algorithm
  20. The false position algorithm uses bracketing by linear interpolation.
  21. If a linear interpolation step would decrease the size of the
  22. bracket by less than a bisection step would then the algorithm
  23. takes a bisection step instead.
  24. The last linear interpolation estimate of the root is used. If a
  25. bisection step causes it to fall outside the brackets then it is
  26. replaced by the bisection estimate (x_upper + x_lower)/2.
  27. */
  28. #include "gsl__config.h"
  29. #include <stddef.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <math.h>
  33. #include <float.h>
  34. #include "gsl_math.h"
  35. #include "gsl_errno.h"
  36. #include "gsl_roots.h"
  37. #include "gsl_roots__roots.h"
  38. typedef struct
  39. {
  40. double f_lower, f_upper;
  41. }
  42. falsepos_state_t;
  43. static int falsepos_init (void * vstate, gsl_function * f, double * root, double x_lower, double x_upper);
  44. static int falsepos_iterate (void * vstate, gsl_function * f, double * root, double * x_lower, double * x_upper);
  45. static int
  46. falsepos_init (void * vstate, gsl_function * f, double * root, double x_lower, double x_upper)
  47. {
  48. falsepos_state_t * state = (falsepos_state_t *) vstate;
  49. double f_lower, f_upper ;
  50. *root = 0.5 * (x_lower + x_upper);
  51. SAFE_FUNC_CALL (f, x_lower, &f_lower);
  52. SAFE_FUNC_CALL (f, x_upper, &f_upper);
  53. state->f_lower = f_lower;
  54. state->f_upper = f_upper;
  55. if ((f_lower < 0.0 && f_upper < 0.0) || (f_lower > 0.0 && f_upper > 0.0))
  56. {
  57. GSL_ERROR ("endpoints do not straddle y=0", GSL_EINVAL);
  58. }
  59. return GSL_SUCCESS;
  60. }
  61. static int
  62. falsepos_iterate (void * vstate, gsl_function * f, double * root, double * x_lower, double * x_upper)
  63. {
  64. falsepos_state_t * state = (falsepos_state_t *) vstate;
  65. double x_linear, f_linear;
  66. double x_bisect, f_bisect;
  67. double x_left = *x_lower ;
  68. double x_right = *x_upper ;
  69. double f_lower = state->f_lower;
  70. double f_upper = state->f_upper;
  71. double w ;
  72. if (f_lower == 0.0)
  73. {
  74. *root = x_left ;
  75. *x_upper = x_left;
  76. return GSL_SUCCESS;
  77. }
  78. if (f_upper == 0.0)
  79. {
  80. *root = x_right ;
  81. *x_lower = x_right;
  82. return GSL_SUCCESS;
  83. }
  84. /* Draw a line between f(*lower_bound) and f(*upper_bound) and
  85. note where it crosses the X axis; that's where we will
  86. split the interval. */
  87. x_linear = x_right - (f_upper * (x_left - x_right) / (f_lower - f_upper));
  88. SAFE_FUNC_CALL (f, x_linear, &f_linear);
  89. if (f_linear == 0.0)
  90. {
  91. *root = x_linear;
  92. *x_lower = x_linear;
  93. *x_upper = x_linear;
  94. return GSL_SUCCESS;
  95. }
  96. /* Discard the half of the interval which doesn't contain the root. */
  97. if ((f_lower > 0.0 && f_linear < 0.0) || (f_lower < 0.0 && f_linear > 0.0))
  98. {
  99. *root = x_linear ;
  100. *x_upper = x_linear;
  101. state->f_upper = f_linear;
  102. w = x_linear - x_left ;
  103. }
  104. else
  105. {
  106. *root = x_linear ;
  107. *x_lower = x_linear;
  108. state->f_lower = f_linear;
  109. w = x_right - x_linear;
  110. }
  111. if (w < 0.5 * (x_right - x_left))
  112. {
  113. return GSL_SUCCESS ;
  114. }
  115. x_bisect = 0.5 * (x_left + x_right);
  116. SAFE_FUNC_CALL (f, x_bisect, &f_bisect);
  117. if ((f_lower > 0.0 && f_bisect < 0.0) || (f_lower < 0.0 && f_bisect > 0.0))
  118. {
  119. *x_upper = x_bisect;
  120. state->f_upper = f_bisect;
  121. if (*root > x_bisect)
  122. *root = 0.5 * (x_left + x_bisect) ;
  123. }
  124. else
  125. {
  126. *x_lower = x_bisect;
  127. state->f_lower = f_bisect;
  128. if (*root < x_bisect)
  129. *root = 0.5 * (x_bisect + x_right) ;
  130. }
  131. return GSL_SUCCESS;
  132. }
  133. static const gsl_root_fsolver_type falsepos_type =
  134. {"falsepos", /* name */
  135. sizeof (falsepos_state_t),
  136. &falsepos_init,
  137. &falsepos_iterate};
  138. const gsl_root_fsolver_type * gsl_root_fsolver_falsepos = &falsepos_type;