gsl_roots__newton.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* roots/newton.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. /* newton.c -- newton root finding algorithm
  20. This is the classical Newton-Raphson iteration.
  21. x[i+1] = x[i] - f(x[i])/f'(x[i])
  22. */
  23. #include "gsl__config.h"
  24. #include <stddef.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include <float.h>
  29. #include "gsl_math.h"
  30. #include "gsl_errno.h"
  31. #include "gsl_roots.h"
  32. #include "gsl_roots__roots.h"
  33. typedef struct
  34. {
  35. double f, df;
  36. }
  37. newton_state_t;
  38. static int newton_init (void * vstate, gsl_function_fdf * fdf, double * root);
  39. static int newton_iterate (void * vstate, gsl_function_fdf * fdf, double * root);
  40. static int
  41. newton_init (void * vstate, gsl_function_fdf * fdf, double * root)
  42. {
  43. newton_state_t * state = (newton_state_t *) vstate;
  44. const double x = *root ;
  45. state->f = GSL_FN_FDF_EVAL_F (fdf, x);
  46. state->df = GSL_FN_FDF_EVAL_DF (fdf, x) ;
  47. return GSL_SUCCESS;
  48. }
  49. static int
  50. newton_iterate (void * vstate, gsl_function_fdf * fdf, double * root)
  51. {
  52. newton_state_t * state = (newton_state_t *) vstate;
  53. double root_new, f_new, df_new;
  54. if (state->df == 0.0)
  55. {
  56. GSL_ERROR("derivative is zero", GSL_EZERODIV);
  57. }
  58. root_new = *root - (state->f / state->df);
  59. *root = root_new ;
  60. GSL_FN_FDF_EVAL_F_DF(fdf, root_new, &f_new, &df_new);
  61. state->f = f_new ;
  62. state->df = df_new ;
  63. if (!gsl_finite(f_new))
  64. {
  65. GSL_ERROR ("function value is not finite", GSL_EBADFUNC);
  66. }
  67. if (!gsl_finite (df_new))
  68. {
  69. GSL_ERROR ("derivative value is not finite", GSL_EBADFUNC);
  70. }
  71. return GSL_SUCCESS;
  72. }
  73. static const gsl_root_fdfsolver_type newton_type =
  74. {"newton", /* name */
  75. sizeof (newton_state_t),
  76. &newton_init,
  77. &newton_iterate};
  78. const gsl_root_fdfsolver_type * gsl_root_fdfsolver_newton = &newton_type;