gsl_roots__steffenson.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* roots/steffenson.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. /* steffenson.c -- steffenson root finding algorithm
  20. This is Newton's method with an Aitken "delta-squared"
  21. acceleration of the iterates. This can improve the convergence on
  22. multiple roots where the ordinary Newton algorithm is slow.
  23. x[i+1] = x[i] - f(x[i]) / f'(x[i])
  24. x_accelerated[i] = x[i] - (x[i+1] - x[i])**2 / (x[i+2] - 2*x[i+1] + x[i])
  25. We can only use the accelerated estimate after three iterations,
  26. and use the unaccelerated value until then.
  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, df;
  41. double x;
  42. double x_1;
  43. double x_2;
  44. int count;
  45. }
  46. steffenson_state_t;
  47. static int steffenson_init (void * vstate, gsl_function_fdf * fdf, double * root);
  48. static int steffenson_iterate (void * vstate, gsl_function_fdf * fdf, double * root);
  49. static int
  50. steffenson_init (void * vstate, gsl_function_fdf * fdf, double * root)
  51. {
  52. steffenson_state_t * state = (steffenson_state_t *) vstate;
  53. const double x = *root ;
  54. state->f = GSL_FN_FDF_EVAL_F (fdf, x);
  55. state->df = GSL_FN_FDF_EVAL_DF (fdf, x) ;
  56. state->x = x;
  57. state->x_1 = 0.0;
  58. state->x_2 = 0.0;
  59. state->count = 1;
  60. return GSL_SUCCESS;
  61. }
  62. static int
  63. steffenson_iterate (void * vstate, gsl_function_fdf * fdf, double * root)
  64. {
  65. steffenson_state_t * state = (steffenson_state_t *) vstate;
  66. double x_new, f_new, df_new;
  67. double x_1 = state->x_1 ;
  68. double x = state->x ;
  69. if (state->df == 0.0)
  70. {
  71. GSL_ERROR("derivative is zero", GSL_EZERODIV);
  72. }
  73. x_new = x - (state->f / state->df);
  74. GSL_FN_FDF_EVAL_F_DF(fdf, x_new, &f_new, &df_new);
  75. state->x_2 = x_1 ;
  76. state->x_1 = x ;
  77. state->x = x_new;
  78. state->f = f_new ;
  79. state->df = df_new ;
  80. if (!gsl_finite (f_new))
  81. {
  82. GSL_ERROR ("function value is not finite", GSL_EBADFUNC);
  83. }
  84. if (state->count < 3)
  85. {
  86. *root = x_new ;
  87. state->count++ ;
  88. }
  89. else
  90. {
  91. double u = (x - x_1) ;
  92. double v = (x_new - 2 * x + x_1);
  93. if (v == 0)
  94. *root = x_new; /* avoid division by zero */
  95. else
  96. *root = x_1 - u * u / v ; /* accelerated value */
  97. }
  98. if (!gsl_finite (df_new))
  99. {
  100. GSL_ERROR ("derivative value is not finite", GSL_EBADFUNC);
  101. }
  102. return GSL_SUCCESS;
  103. }
  104. static const gsl_root_fdfsolver_type steffenson_type =
  105. {"steffenson", /* name */
  106. sizeof (steffenson_state_t),
  107. &steffenson_init,
  108. &steffenson_iterate};
  109. const gsl_root_fdfsolver_type * gsl_root_fdfsolver_steffenson = &steffenson_type;