help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] Re: Numerical Gradient?


From: Thad Harroun
Subject: [Help-gsl] Re: Numerical Gradient?
Date: Thu, 06 Mar 2008 15:56:18 -0500
User-agent: Thunderbird 2.0.0.6 (X11/20071022)

I saw this implementation somewhere, I forget where, and use it a lot. I
hope the person who thought this up is still reading the list.

You have an array of parameters: parameter_array[n]
I like to keep it in a my_struct which is passed as the void pointer by
the GSL fdfminimizer solver into my_f and my_fdf functions.

You have a function to be minimized: my_func(void *my_struct)

The array is copied into a gsl_vector and fed into the fdfminimizer as
the initial guess.  It also comes back to you every iteration of the solver.

So, in my_fdf, has this psuedocode:
====================================================
void my_df(const gsl_vector *updated_parameters,
                       void *my_struct,
                       gsl_vector *df) << derivatives go here
{
 Load the updated_parameters into a my_struct->temporary_array
 << this reflects the current state of the minmization

 gsl_function F = my_derivatives;
 << a required function to answer calls of the numerical derivatives

 Loop over the paramters i=1->n
 {
  my_struct->index = i;
  if (parameter is held fixed) derivative = 0
  else gsl_deriv_central(&F, my_struct->temporary_array[i], \\
                         tolerence, &derivative, &error);
  gsl_vector_set(df, i, derivative);
 }
}

The function my_derivatives looks like this:
====================================================
double my_derivatives(double p,
                      void *my_struct)
{
 Load my_struct->temporary_array into my_struct->parameter_array
    << reflects the current state of the minmization
 my_struct->parameter_array[my_struct->index] = p;
    << we are taking the derivative of this parameter

 return my_func(void *my_struct)
    << call the function to be minimized, which must use
    << parameter_array to do its calculations.
}

Finally, the very general function my_func:
====================================================
double my_func(void *my_struct)
 << Returns the value of the function to be minimized, using parameter_array


Hope that helps,

Thad

---------------------------
Department of Physics
Brock University
St. Catharines, ON  L2S 3A1
Canada
Phone: 905-688-5550 x4294




reply via email to

[Prev in Thread] Current Thread [Next in Thread]