Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numerical integration of smooth unimodal function with GSL?

Can someone post a simple example of numerical integration of a smooth unimodal function in a finite interval with GSL?

like image 660
becko Avatar asked Oct 28 '25 21:10

becko


1 Answers

heres an example, integrating 1/(t^2 + 1) over [0,1000]. It uses adaptive integration with the simplest ruleset since there are no singularities.

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>

double f (double x, void * params) {
    double alpha = *(double *) params;
    double f = alpha / (x * x + 1);
    return f;
}

    int
main (void)
{
    gsl_integration_workspace * w 
        = gsl_integration_workspace_alloc (1000);

    double result, error;
    double alpha = 1.0;


    gsl_function F;
    F.function = &f;
    F.params = &alpha;

    gsl_integration_qag (&F,
                         0.0, 1000.0,
                         0.0, 1e-7, 1000,
                         GSL_INTEG_GAUSS15,
                         w,
                         &result, &error);

    printf ("result          = % .18f\n", result);
    printf ("estimated error = % .18f\n", error);

    gsl_integration_workspace_free (w);

    return 0;
} 

And the results are

result          =  1.569796327128230029
estimated error =  0.000000000092546021

Which makes sense, since the integral should be about pi/2.

like image 106
Steve Cox Avatar answered Oct 31 '25 13:10

Steve Cox