Go to the first, previous, next, last section, table of contents.

Providing a function to minimize

You must provide a parametric function of @math{n} variables for the minimizers to operate on. You also need to provide a routine which calculates the gradient of the function and a third routine which calculates both the function value and the gradient together. In order to allow for general parameters the functions are defined by the following data type:

Data Type: gsl_multimin_function_fdf
This data type defines a general function of @math{n} variables with parameters and the corresponding gradient vector of derivatives,

double (* f) (const gsl_vector * x, void * params)
this function should return the result @math{f(x,params)} for argument x and parameters params.
int (* df) (const gsl_vector * x, void * params, gsl_vector * g)
this function should store the n-dimensional gradient @math{g_i = d f(x,params) / d x_i} in the vector g for argument x and parameters params, returning an appropriate error code if the function cannot be computed.
int (* fdf) (const gsl_vector * x, void * params, double * f, gsl_vector * g)
This function should set the values of the f and g as above, for arguments x and parameters params. This function provides an optimization of the separate functions for @math{f(x)} and @math{g(x)} -- it is always faster to compute the function and its derivative at the same time.
size_t n
the dimension of the system, i.e. the number of components of the vectors x.
void * params
a pointer to the parameters of the function.

The following example function defines a simple paraboloid with two parameters,

/* Paraboloid centered on (dp[0],dp[1]) */

double
my_f (const gsl_vector *v, void *params)
{
  double x, y;
  double *dp = (double *)params;
  
  x = gsl_vector_get(v, 0);
  y = gsl_vector_get(v, 1);
 
  return 10.0 * (x - dp[0]) * (x - dp[0]) +
           20.0 * (y - dp[1]) * (y - dp[1]) + 30.0; 
}

/* The gradient of f, df = (df/dx, df/dy). */
void 
my_df (const gsl_vector *v, void *params, 
       gsl_vector *df)
{
  double x, y;
  double *dp = (double *)params;
  
  x = gsl_vector_get(v, 0);
  y = gsl_vector_get(v, 1);
 
  gsl_vector_set(df, 0, 20.0 * (x - dp[0]));
  gsl_vector_set(df, 1, 40.0 * (y - dp[1]));
}

/* Compute both f and df together. */
void 
my_fdf (const gsl_vector *x, void *params, 
        double *f, gsl_vector *df) 
{
  *f = my_f(x, params); 
  my_df(x, params, df);
}

The function can be initialized using the following code,

gsl_multimin_function_fdf my_func;

double p[2] = { 1.0, 2.0 }; /* center at (1,2) */

my_func.f = &my_f;
my_func.df = &my_df;
my_func.fdf = &my_fdf;
my_func.n = 2;
my_func.params = (void *)p;

Go to the first, previous, next, last section, table of contents.