1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/* roots/steffenson.c
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, Brian Gough
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* steffenson.c -- steffenson root finding algorithm
This is Newton's method with an Aitken "delta-squared"
acceleration of the iterates. This can improve the convergence on
multiple roots where the ordinary Newton algorithm is slow.
x[i+1] = x[i] - f(x[i]) / f'(x[i])
x_accelerated[i] = x[i] - (x[i+1] - x[i])**2 / (x[i+2] - 2*x[i+1] + x[i])
We can only use the accelerated estimate after three iterations,
and use the unaccelerated value until then.
*/
#include <config.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_roots.h>
#include "roots.h"
typedef struct
{
double f, df;
double x;
double x_1;
double x_2;
int count;
}
steffenson_state_t;
static int steffenson_init (void * vstate, gsl_function_fdf * fdf, double * root);
static int steffenson_iterate (void * vstate, gsl_function_fdf * fdf, double * root);
static int
steffenson_init (void * vstate, gsl_function_fdf * fdf, double * root)
{
steffenson_state_t * state = (steffenson_state_t *) vstate;
const double x = *root ;
state->f = GSL_FN_FDF_EVAL_F (fdf, x);
state->df = GSL_FN_FDF_EVAL_DF (fdf, x) ;
state->x = x;
state->x_1 = 0.0;
state->x_2 = 0.0;
state->count = 1;
return GSL_SUCCESS;
}
static int
steffenson_iterate (void * vstate, gsl_function_fdf * fdf, double * root)
{
steffenson_state_t * state = (steffenson_state_t *) vstate;
double x_new, f_new, df_new;
double x_1 = state->x_1 ;
double x = state->x ;
if (state->df == 0.0)
{
GSL_ERROR("derivative is zero", GSL_EZERODIV);
}
x_new = x - (state->f / state->df);
GSL_FN_FDF_EVAL_F_DF(fdf, x_new, &f_new, &df_new);
state->x_2 = x_1 ;
state->x_1 = x ;
state->x = x_new;
state->f = f_new ;
state->df = df_new ;
if (!gsl_finite (f_new))
{
GSL_ERROR ("function value is not finite", GSL_EBADFUNC);
}
if (state->count < 3)
{
*root = x_new ;
state->count++ ;
}
else
{
double u = (x - x_1) ;
double v = (x_new - 2 * x + x_1);
if (v == 0)
*root = x_new; /* avoid division by zero */
else
*root = x_1 - u * u / v ; /* accelerated value */
}
if (!gsl_finite (df_new))
{
GSL_ERROR ("derivative value is not finite", GSL_EBADFUNC);
}
return GSL_SUCCESS;
}
static const gsl_root_fdfsolver_type steffenson_type =
{"steffenson", /* name */
sizeof (steffenson_state_t),
&steffenson_init,
&steffenson_iterate};
const gsl_root_fdfsolver_type * gsl_root_fdfsolver_steffenson = &steffenson_type;
|