Blame roots/steffenson.c

Packit 67cb25
/* roots/steffenson.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
/* steffenson.c -- steffenson root finding algorithm 
Packit 67cb25
Packit 67cb25
   This is Newton's method with an Aitken "delta-squared"
Packit 67cb25
   acceleration of the iterates. This can improve the convergence on
Packit 67cb25
   multiple roots where the ordinary Newton algorithm is slow.
Packit 67cb25
Packit 67cb25
   x[i+1] = x[i] - f(x[i]) / f'(x[i])
Packit 67cb25
Packit 67cb25
   x_accelerated[i] = x[i] - (x[i+1] - x[i])**2 / (x[i+2] - 2*x[i+1] + x[i])
Packit 67cb25
Packit 67cb25
   We can only use the accelerated estimate after three iterations,
Packit 67cb25
   and use the unaccelerated value until then.
Packit 67cb25
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
Packit 67cb25
#include <stddef.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <stdio.h>
Packit 67cb25
#include <math.h>
Packit 67cb25
#include <float.h>
Packit 67cb25
Packit 67cb25
#include <gsl/gsl_math.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
#include <gsl/gsl_roots.h>
Packit 67cb25
Packit 67cb25
#include "roots.h"
Packit 67cb25
Packit 67cb25
typedef struct
Packit 67cb25
  {
Packit 67cb25
    double f, df;
Packit 67cb25
    double x;
Packit 67cb25
    double x_1;
Packit 67cb25
    double x_2;
Packit 67cb25
    int count;
Packit 67cb25
  }
Packit 67cb25
steffenson_state_t;
Packit 67cb25
Packit 67cb25
static int steffenson_init (void * vstate, gsl_function_fdf * fdf, double * root);
Packit 67cb25
static int steffenson_iterate (void * vstate, gsl_function_fdf * fdf, double * root);
Packit 67cb25
Packit 67cb25
static int
Packit 67cb25
steffenson_init (void * vstate, gsl_function_fdf * fdf, double * root)
Packit 67cb25
{
Packit 67cb25
  steffenson_state_t * state = (steffenson_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  const double x = *root ;
Packit 67cb25
Packit 67cb25
  state->f = GSL_FN_FDF_EVAL_F (fdf, x);
Packit 67cb25
  state->df = GSL_FN_FDF_EVAL_DF (fdf, x) ;
Packit 67cb25
Packit 67cb25
  state->x = x;
Packit 67cb25
  state->x_1 = 0.0;
Packit 67cb25
  state->x_2 = 0.0;
Packit 67cb25
Packit 67cb25
  state->count = 1;
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static int
Packit 67cb25
steffenson_iterate (void * vstate, gsl_function_fdf * fdf, double * root)
Packit 67cb25
{
Packit 67cb25
  steffenson_state_t * state = (steffenson_state_t *) vstate;
Packit 67cb25
  
Packit 67cb25
  double x_new, f_new, df_new;
Packit 67cb25
Packit 67cb25
  double x_1 = state->x_1 ;
Packit 67cb25
  double x = state->x ;
Packit 67cb25
Packit 67cb25
  if (state->df == 0.0)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR("derivative is zero", GSL_EZERODIV);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  x_new = x - (state->f / state->df);
Packit 67cb25
  
Packit 67cb25
  GSL_FN_FDF_EVAL_F_DF(fdf, x_new, &f_new, &df_new);
Packit 67cb25
Packit 67cb25
  state->x_2 = x_1 ;
Packit 67cb25
  state->x_1 = x ;
Packit 67cb25
  state->x = x_new;
Packit 67cb25
Packit 67cb25
  state->f = f_new ;
Packit 67cb25
  state->df = df_new ;
Packit 67cb25
Packit 67cb25
  if (!gsl_finite (f_new))
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("function value is not finite", GSL_EBADFUNC);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  if (state->count < 3)
Packit 67cb25
    {
Packit 67cb25
      *root = x_new ;
Packit 67cb25
      state->count++ ;
Packit 67cb25
    }
Packit 67cb25
  else 
Packit 67cb25
    {
Packit 67cb25
      double u = (x - x_1) ;
Packit 67cb25
      double v = (x_new - 2 * x + x_1);
Packit 67cb25
Packit 67cb25
      if (v == 0)
Packit 67cb25
        *root = x_new;  /* avoid division by zero */
Packit 67cb25
      else
Packit 67cb25
        *root = x_1 - u * u / v ;  /* accelerated value */
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  if (!gsl_finite (df_new))
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("derivative value is not finite", GSL_EBADFUNC);
Packit 67cb25
    }
Packit 67cb25
      
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
Packit 67cb25
static const gsl_root_fdfsolver_type steffenson_type =
Packit 67cb25
{"steffenson",                          /* name */
Packit 67cb25
 sizeof (steffenson_state_t),
Packit 67cb25
 &steffenson_init,
Packit 67cb25
 &steffenson_iterate};
Packit 67cb25
Packit 67cb25
const gsl_root_fdfsolver_type  * gsl_root_fdfsolver_steffenson = &steffenson_type;