3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 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.
21 /* goldensection.c -- goldensection minimum finding algorithm */
31 #include <gsl/gsl_math.h>
32 #include <gsl/gsl_errno.h>
33 #include <gsl/gsl_min.h>
41 goldensection_state_t;
43 static int goldensection_init (void * vstate, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper);
44 static int goldensection_iterate (void * vstate, gsl_function * f, double * x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper);
47 goldensection_init (void * vstate, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper)
49 goldensection_state_t * state = (goldensection_state_t *) vstate;
51 /* no initialization required, prevent warnings about unused variables */
66 goldensection_iterate (void * vstate, gsl_function * f, double * x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper)
68 goldensection_state_t * state = (goldensection_state_t *) vstate;
70 const double x_center = *x_minimum ;
71 const double x_left = *x_lower ;
72 const double x_right = *x_upper ;
74 const double f_min = *f_minimum;
76 const double golden = 0.3819660; /* golden = (3 - sqrt(5))/2 */
78 const double w_lower = (x_center - x_left);
79 const double w_upper = (x_right - x_center);
83 state = 0 ; /* avoid warning about unused parameters */
85 x_new = x_center + golden * ((w_upper > w_lower) ? w_upper : -w_lower) ;
87 SAFE_FUNC_CALL (f, x_new, &f_new);
95 else if (x_new < x_center && f_new > f_min)
101 else if (x_new > x_center && f_new > f_min)
114 static const gsl_min_fminimizer_type goldensection_type =
115 {"goldensection", /* name */
116 sizeof (goldensection_state_t),
118 &goldensection_iterate};
120 const gsl_min_fminimizer_type * gsl_min_fminimizer_goldensection = &goldensection_type;