help-gsl
[Top][All Lists]
Advanced

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

[Help-gsl] Passing a user function to Brent


From: Martin De Kauwe
Subject: [Help-gsl] Passing a user function to Brent
Date: Thu, 3 Dec 2009 00:27:36 +0000

Hi,

I am trying to setup my own function that I can use the GSL brent function 
with, but I am not sure I have done it correctly as it my code doesn't get past 
the gsl_min_fminimizer_set step. I can replicate the example on the website 
fine, I just seem to run into trouble when I try to adjust it to suit my needs. 
Any suggestions would be appreciated. When I call the code with

min = 0.367879 
lower = -2.000000 
upper = 2.000000

It seems to call the least squares func 4 times and then crash.

88.320825
88.320825
88.320825

gsl: fsolver.c:128: ERROR: endpoints do not enclose a minimum
Default GSL error handler invoked.
Abort trap

Code snippets.

int gsl_brent(control *c, timeseries *ts, double lower, double minimum, double 
upper) {

        int    status;
        int    iter = 0, max_iter = 1000;
        double tolerance = 1E-6;
        const  gsl_min_fminimizer_type *T;
   
        gsl_min_fminimizer *s;
        gsl_function F;
        
        T = gsl_min_fminimizer_brent;
        
        /* put all the stuff in one big strucutre so the GSL will accept it*/
        meta *m;
        m = (meta *)malloc(sizeof(meta));
        m->time_series = ts;
        m->controls    = c;
        m->min = minimum;
        
        /* set up the user function structure*/
        F.function = &mini_gsl_wrapper;
        F.params = (void *)m;
        
        /*setup the minimaizer*/ 
        s = gsl_min_fminimizer_alloc(T);
        gsl_min_fminimizer_set(s, &F, minimum, lower, upper);
        
        do {
                iter++;
                status = gsl_min_fminimizer_iterate(s);
                
                minimum = gsl_min_fminimizer_x_minimum(s);
                lower = gsl_min_fminimizer_x_lower(s);
                upper = gsl_min_fminimizer_x_upper(s);

                status = gsl_min_test_interval(lower, upper, tolerance, 1e-06);

                if (status == GSL_SUCCESS) {
                        printf("Converged:\n");
                }
                /* make sure my func is getting the updated minimum, not sure 
if i need to do this? */
                m->min = minimum;
                
        } while(status == GSL_CONTINUE && iter < max_iter);

        gsl_min_fminimizer_free(s);
        
        return status;
        
}
double mini_gsl_wrapper(double ls, void *params)
{
        meta *m  = (meta *)params;
        
        ls = least_sqs(m->min, m->time_series->nseg, m->time_series->tscal, 
m->time_series->xscal);
        return (ls);
} 


Thanks Martin



reply via email to

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