Blame roots/falsepos.c

Packit 67cb25
/* roots/falsepos.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
/* falsepos.c -- falsepos root finding algorithm 
Packit 67cb25
Packit 67cb25
   The false position algorithm uses bracketing by linear interpolation.
Packit 67cb25
Packit 67cb25
   If a linear interpolation step would decrease the size of the
Packit 67cb25
   bracket by less than a bisection step would then the algorithm
Packit 67cb25
   takes a bisection step instead.
Packit 67cb25
   
Packit 67cb25
   The last linear interpolation estimate of the root is used. If a
Packit 67cb25
   bisection step causes it to fall outside the brackets then it is
Packit 67cb25
   replaced by the bisection estimate (x_upper + x_lower)/2.
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_lower, f_upper;
Packit 67cb25
  }
Packit 67cb25
falsepos_state_t;
Packit 67cb25
Packit 67cb25
static int falsepos_init (void * vstate, gsl_function * f, double * root, double x_lower, double x_upper);
Packit 67cb25
static int falsepos_iterate (void * vstate, gsl_function * f, double * root, double * x_lower, double * x_upper);
Packit 67cb25
Packit 67cb25
static int
Packit 67cb25
falsepos_init (void * vstate, gsl_function * f, double * root, double x_lower, double x_upper)
Packit 67cb25
{
Packit 67cb25
  falsepos_state_t * state = (falsepos_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  double f_lower, f_upper ;
Packit 67cb25
Packit 67cb25
  *root = 0.5 * (x_lower + x_upper);
Packit 67cb25
Packit 67cb25
  SAFE_FUNC_CALL (f, x_lower, &f_lower);
Packit 67cb25
  SAFE_FUNC_CALL (f, x_upper, &f_upper);
Packit 67cb25
  
Packit 67cb25
  state->f_lower = f_lower;
Packit 67cb25
  state->f_upper = f_upper;
Packit 67cb25
Packit 67cb25
  if ((f_lower < 0.0 && f_upper < 0.0) || (f_lower > 0.0 && f_upper > 0.0))
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("endpoints do not straddle y=0", GSL_EINVAL);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static int
Packit 67cb25
falsepos_iterate (void * vstate, gsl_function * f, double * root, double * x_lower, double * x_upper)
Packit 67cb25
{
Packit 67cb25
  falsepos_state_t * state = (falsepos_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  double x_linear, f_linear;
Packit 67cb25
  double x_bisect, f_bisect;
Packit 67cb25
Packit 67cb25
  double x_left = *x_lower ;
Packit 67cb25
  double x_right = *x_upper ;
Packit 67cb25
Packit 67cb25
  double f_lower = state->f_lower; 
Packit 67cb25
  double f_upper = state->f_upper;
Packit 67cb25
Packit 67cb25
  double w ;
Packit 67cb25
Packit 67cb25
  if (f_lower == 0.0)
Packit 67cb25
    {
Packit 67cb25
      *root = x_left ;
Packit 67cb25
      *x_upper = x_left;
Packit 67cb25
      return GSL_SUCCESS;
Packit 67cb25
    }
Packit 67cb25
  
Packit 67cb25
  if (f_upper == 0.0)
Packit 67cb25
    {
Packit 67cb25
      *root = x_right ;
Packit 67cb25
      *x_lower = x_right;
Packit 67cb25
      return GSL_SUCCESS;
Packit 67cb25
    }
Packit 67cb25
      
Packit 67cb25
  /* Draw a line between f(*lower_bound) and f(*upper_bound) and
Packit 67cb25
     note where it crosses the X axis; that's where we will
Packit 67cb25
     split the interval. */
Packit 67cb25
  
Packit 67cb25
  x_linear = x_right - (f_upper * (x_left - x_right) / (f_lower - f_upper));
Packit 67cb25
Packit 67cb25
  SAFE_FUNC_CALL (f, x_linear, &f_linear);
Packit 67cb25
      
Packit 67cb25
  if (f_linear == 0.0)
Packit 67cb25
    {
Packit 67cb25
      *root = x_linear;
Packit 67cb25
      *x_lower = x_linear;
Packit 67cb25
      *x_upper = x_linear;
Packit 67cb25
      return GSL_SUCCESS;
Packit 67cb25
    }
Packit 67cb25
      
Packit 67cb25
  /* Discard the half of the interval which doesn't contain the root. */
Packit 67cb25
  
Packit 67cb25
  if ((f_lower > 0.0 && f_linear < 0.0) || (f_lower < 0.0 && f_linear > 0.0))
Packit 67cb25
    {
Packit 67cb25
      *root = x_linear ;
Packit 67cb25
      *x_upper = x_linear;
Packit 67cb25
      state->f_upper = f_linear;
Packit 67cb25
      w = x_linear - x_left ;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      *root = x_linear ;
Packit 67cb25
      *x_lower = x_linear;
Packit 67cb25
      state->f_lower = f_linear;
Packit 67cb25
      w = x_right - x_linear;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  if (w < 0.5 * (x_right - x_left)) 
Packit 67cb25
    {
Packit 67cb25
      return GSL_SUCCESS ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  x_bisect = 0.5 * (x_left + x_right);
Packit 67cb25
Packit 67cb25
  SAFE_FUNC_CALL (f, x_bisect, &f_bisect);
Packit 67cb25
Packit 67cb25
  if ((f_lower > 0.0 && f_bisect < 0.0) || (f_lower < 0.0 && f_bisect > 0.0))
Packit 67cb25
    {
Packit 67cb25
      *x_upper = x_bisect;
Packit 67cb25
      state->f_upper = f_bisect;
Packit 67cb25
      if (*root > x_bisect)
Packit 67cb25
        *root = 0.5 * (x_left + x_bisect) ;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      *x_lower = x_bisect;
Packit 67cb25
      state->f_lower = f_bisect;
Packit 67cb25
      if (*root < x_bisect)
Packit 67cb25
        *root = 0.5 * (x_bisect + x_right) ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
Packit 67cb25
static const gsl_root_fsolver_type falsepos_type =
Packit 67cb25
{"falsepos",                            /* name */
Packit 67cb25
 sizeof (falsepos_state_t),
Packit 67cb25
 &falsepos_init,
Packit 67cb25
 &falsepos_iterate};
Packit 67cb25
Packit 67cb25
const gsl_root_fsolver_type  * gsl_root_fsolver_falsepos = &falsepos_type;