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 /* secant.c -- secant root finding algorithm
22 The secant algorithm is a variant of the Newton algorithm with the
23 derivative term replaced by a numerical estimate from the last two
26 x[i+1] = x[i] - f(x[i]) / f'_est
28 where f'_est = (f(x[i]) - f(x[i-1])) / (x[i] - x[i-1])
30 The exact derivative is used for the initial value of f'_est.
42 #include <gsl/gsl_math.h>
43 #include <gsl/gsl_errno.h>
44 #include <gsl/gsl_roots.h>
55 static int secant_init (void * vstate, gsl_function_fdf * fdf, double * root);
56 static int secant_iterate (void * vstate, gsl_function_fdf * fdf, double * root);
59 secant_init (void * vstate, gsl_function_fdf * fdf, double * root)
61 secant_state_t * state = (secant_state_t *) vstate;
63 const double x = *root;
65 GSL_FN_FDF_EVAL_F_DF (fdf, x, &(state->f), &(state->df));
72 secant_iterate (void * vstate, gsl_function_fdf * fdf, double * root)
74 secant_state_t * state = (secant_state_t *) vstate;
76 const double x = *root ;
77 const double f = state->f;
78 const double df = state->df;
80 double x_new, f_new, df_new;
84 GSL_ERROR("derivative is zero", GSL_EZERODIV);
89 f_new = GSL_FN_FDF_EVAL_F(fdf, x_new) ;
90 df_new = (f_new - f) / (x_new - x) ;
97 if (!gsl_finite (f_new))
99 GSL_ERROR ("function value is not finite", GSL_EBADFUNC);
102 if (!gsl_finite (df_new))
104 GSL_ERROR ("derivative value is not finite", GSL_EBADFUNC);
111 static const gsl_root_fdfsolver_type secant_type =
112 {"secant", /* name */
113 sizeof (secant_state_t),
117 const gsl_root_fdfsolver_type * gsl_root_fdfsolver_secant = &secant_type;