help-gsl
[Top][All Lists]
Advanced

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

Re: [Help-gsl] integrating a function with externally defined coefficien


From: Joakim Hove
Subject: Re: [Help-gsl] integrating a function with externally defined coefficients
Date: Sat, 12 Feb 2005 20:48:33 +0100
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.2 (gnu/linux)

Nathan Moore <address@hidden> writes:

> I'd like to use GSL to numerically integrate a function with a bunch
> of coefficients that I defined as "global" variables.  At present the
> integration doesn't work, which I imagine might have something to do
> with the "gsl_function" type - which I don't really understand.

The gsl_function type is a struct with a function pointer, pointing to
the function which should be called, *and* a void pointer params which
should point to any parameters needed by the function. I.e. to
integrate the function sin(a*x) as a function of x, for various a, you
could do the following:

/* Calling code */
   double   a = 1.0;
   F.function = &sin_ax;
   F.params   = &a;
   ....
   ....

/* User defined funstion sin_ax */

double sin_ax(double x, void *params) {
   double a = (double) (*params);
        
   return sin(a*x);
}

I.e. the params pointer is used to pass in parameters needed for the
function evaluation, which are constant during the
integration/differentiation/minimization/solving/....

For more complex functions, needing more than one parameter, you could
define your own struct holding these parameters, and let param point
to an instance of that type.

I don't know what is wrong with your code - but I generally tend to
(strongly) dislike global variables. Personally I would do something
like this:

double length_function(double t, void *params) {
     
     double *Ax = (double *) params;
     /* 
        Assuming pie = 3.141592... I would replace it with the
        the constant M_PI in <gsl/gsl_math.h>.
     */

     Remeinaing part of function unchanged.   
}


main () {
    double Ax[FREQUENCY_LIMIT];
    /* initialise Ax + alloc workspace +++ */
    F.function = length_function;
    F.params   = Ax; 

    Rest of main is unchanged 

}

HTH Joakim



-- 
Joakim Hove
hove AT ift uib no
Tlf: +47 (55 5)8 27 90 
Fax: +47 (55 5)8 94 40
http://www.ift.uib.no/~hove/





reply via email to

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