Next: Multimin Iteration, Previous: Initializing the Multidimensional Minimizer, Up: Multidimensional Minimization
You must provide a parametric function of n variables for the minimizers to operate on. You may 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 types:
This data type defines a general function of 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 f(x,params) for argument x and parameters params.
void (* df) (const gsl_vector *
x, void *
params, gsl_vector *
g)
- this function should store the n-dimensional gradient 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.
void (* 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 f(x) and 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.
This data type defines a general function of n variables with parameters,
double (* f) (const gsl_vector *
x, void *
params)
- this function should return the result f(x,params) for argument x and parameters params.
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;