3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, Brian Gough
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.
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.
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.
20 /* steffenson.c -- steffenson root finding algorithm
22 This is Newton's method with an Aitken "delta-squared"
23 acceleration of the iterates. This can improve the convergence on
24 multiple roots where the ordinary Newton algorithm is slow.
26 x[i+1] = x[i] - f(x[i]) / f'(x[i])
28 x_accelerated[i] = x[i] - (x[i+1] - x[i])**2 / (x[i+2] - 2*x[i+1] + x[i])
30 We can only use the accelerated estimate after three iterations,
31 and use the unaccelerated value until then.
43 #include <gsl/gsl_math.h>
44 #include <gsl/gsl_errno.h>
45 #include <gsl/gsl_roots.h>
59 static int steffenson_init (void * vstate, gsl_function_fdf * fdf, double * root);
60 static int steffenson_iterate (void * vstate, gsl_function_fdf * fdf, double * root);
63 steffenson_init (void * vstate, gsl_function_fdf * fdf, double * root)
65 steffenson_state_t * state = (steffenson_state_t *) vstate;
67 const double x = *root ;
69 state->f = GSL_FN_FDF_EVAL_F (fdf, x);
70 state->df = GSL_FN_FDF_EVAL_DF (fdf, x) ;
83 steffenson_iterate (void * vstate, gsl_function_fdf * fdf, double * root)
85 steffenson_state_t * state = (steffenson_state_t *) vstate;
87 double x_new, f_new, df_new;
89 double x_1 = state->x_1 ;
94 GSL_ERROR("derivative is zero", GSL_EZERODIV);
97 x_new = x - (state->f / state->df);
99 GSL_FN_FDF_EVAL_F_DF(fdf, x_new, &f_new, &df_new);
108 if (!gsl_finite (f_new))
110 GSL_ERROR ("function value is not finite", GSL_EBADFUNC);
113 if (state->count < 3)
120 double u = (x - x_1) ;
121 double v = (x_new - 2 * x + x_1);
124 *root = x_new; /* avoid division by zero */
126 *root = x_1 - u * u / v ; /* accelerated value */
129 if (!gsl_finite (df_new))
131 GSL_ERROR ("derivative value is not finite", GSL_EBADFUNC);
138 static const gsl_root_fdfsolver_type steffenson_type =
139 {"steffenson", /* name */
140 sizeof (steffenson_state_t),
142 &steffenson_iterate};
144 const gsl_root_fdfsolver_type * gsl_root_fdfsolver_steffenson = &steffenson_type;