help-gsl
[Top][All Lists]
Advanced

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

[Help-gsl] Numerical Gradient Function


From: David Doria
Subject: [Help-gsl] Numerical Gradient Function
Date: Fri, 14 Mar 2008 10:12:00 -0400

The only thing I saw in the GSL reference manual was a single dimensional
function numerical derivative.  I thought this may be helpful to some folks
who have a multidimensional function that cannot be differentiated.

void NumericalGradient(const gsl_vector *v, void *params, gsl_vector *df)
{
    double CurrentF = my_f(v, params);
    int NumVars = int(v->size);
    vector<double> VarList;
    gsl_vector *temp;
    temp = gsl_vector_alloc (NumVars);
    double epsilon=1e-6;

    //get initial values
    for (int i=0; i < NumVars ; i++)
    {
        VarList.push_back(gsl_vector_get(v, i));
    }

    for (int counter=0; counter < NumVars ; counter++)
    {
        for(int j=0; j< NumVars; j++)
        {
            gsl_vector_set(temp, j, VarList[j]);
        }
        gsl_vector_set(temp,counter,gsl_vector_get(temp,counter) + epsilon);

        gsl_vector_set(df, counter, (my_f(temp, params) -  CurrentF)/epsilon
);
    }

}

-- 
Thanks,

David


reply via email to

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