gsl_roots__secant.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* roots/secant.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. /* secant.c -- secant root finding algorithm
  20. The secant algorithm is a variant of the Newton algorithm with the
  21. derivative term replaced by a numerical estimate from the last two
  22. function evaluations.
  23. x[i+1] = x[i] - f(x[i]) / f'_est
  24. where f'_est = (f(x[i]) - f(x[i-1])) / (x[i] - x[i-1])
  25. The exact derivative is used for the initial value of f'_est.
  26. */
  27. #include "gsl__config.h"
  28. #include <stddef.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include <float.h>
  33. #include "gsl_math.h"
  34. #include "gsl_errno.h"
  35. #include "gsl_roots.h"
  36. #include "gsl_roots__roots.h"
  37. typedef struct
  38. {
  39. double f;
  40. double df;
  41. }
  42. secant_state_t;
  43. static int secant_init (void * vstate, gsl_function_fdf * fdf, double * root);
  44. static int secant_iterate (void * vstate, gsl_function_fdf * fdf, double * root);
  45. static int
  46. secant_init (void * vstate, gsl_function_fdf * fdf, double * root)
  47. {
  48. secant_state_t * state = (secant_state_t *) vstate;
  49. const double x = *root;
  50. GSL_FN_FDF_EVAL_F_DF (fdf, x, &(state->f), &(state->df));
  51. return GSL_SUCCESS;
  52. }
  53. static int
  54. secant_iterate (void * vstate, gsl_function_fdf * fdf, double * root)
  55. {
  56. secant_state_t * state = (secant_state_t *) vstate;
  57. const double x = *root ;
  58. const double f = state->f;
  59. const double df = state->df;
  60. double x_new, f_new, df_new;
  61. if (state->df == 0.0)
  62. {
  63. GSL_ERROR("derivative is zero", GSL_EZERODIV);
  64. }
  65. x_new = x - (f / df);
  66. f_new = GSL_FN_FDF_EVAL_F(fdf, x_new) ;
  67. df_new = (f_new - f) / (x_new - x) ;
  68. *root = x_new ;
  69. state->f = f_new ;
  70. state->df = df_new ;
  71. if (!gsl_finite (f_new))
  72. {
  73. GSL_ERROR ("function value is not finite", GSL_EBADFUNC);
  74. }
  75. if (!gsl_finite (df_new))
  76. {
  77. GSL_ERROR ("derivative value is not finite", GSL_EBADFUNC);
  78. }
  79. return GSL_SUCCESS;
  80. }
  81. static const gsl_root_fdfsolver_type secant_type =
  82. {"secant", /* name */
  83. sizeof (secant_state_t),
  84. &secant_init,
  85. &secant_iterate};
  86. const gsl_root_fdfsolver_type * gsl_root_fdfsolver_secant = &secant_type;