IMSL C Math Library
int_fcn
Integrates a function using a globally adaptive scheme based on Gauss-Kronrod rules.
Synopsis
#include <imsl.h>
float imsl_f_int_fcn (float fcn(), float a, float b, , 0)
The type double function is imsl_d_int_fcn.
Required Arguments
float fcn (float x) (Input)
User-supplied function to be integrated.
float a (Input)
Lower limit of integration.
float b (Input)
Upper limit of integration.
Return Value
The value of
is returned. If no value can be computed, then NaN is returned.
Synopsis with Optional Arguments
#include <imsl.h>
float imsl_f_int_fcn (float fcn(float x), float a, float b,
IMSL_RULE, int rule,
IMSL_ERR_ABS, float err_abs,
IMSL_ERR_REL, float err_rel,
IMSL_ERR_EST, float *err_est,
IMSL_MAX_SUBINTER, int max_subinter,
IMSL_N_SUBINTER, int *n_subinter,
IMSL_N_EVALS, int *n_evals,
IMSL_FCN_W_DATA, float fcn(), void *data,
0)
Optional Arguments
IMSL_RULE, int rule (Input)
Choice of quadrature rule.
rule
Gauss-Kronrod Rule
1
7-15 points
2
10-21 points
3
15-31 points
4
20-41 points
5
25-51 points
6
30-61 points
Default: rule = 1
IMSL_ERR_ABS, float err_abs (Input)
Absolute accuracy desired.
Default: where ɛ is the machine precision
IMSL_ERR_REL, float err_rel (Input)
Relative accuracy desired.
Default: where ɛ is the machine precision
IMSL_ERR_EST, float *err_est (Output)
Address to store an estimate of the absolute value of the error.
IMSL_MAX_SUBINTER, int max_subinter (Input)
Number of subintervals allowed.
Default: max_subinter = 500
IMSL_N_SUBINTER, int *n_subinter (Output)
Address to store the number of subintervals generated.
IMSL_N_EVALS, int *n_evals (Output)
Address to store the number of evaluations of fcn.
IMSL_FCN_W_DATA, float fcn (float x, void *data), void *data (Input)
User supplied function to be integrated, which also accepts a pointer to data that is supplied by the user. data is a pointer to the data to be passed to the user-supplied function. See Passing Data to User-Supplied Functions in the introduction to this manual for more details.
Description
The function imsl_f_int_fcn is a general-purpose integrator that uses a globally adaptive scheme to reduce the absolute error. It subdivides the interval [a, b] and uses a (2k + 1)-point Gauss-Kronrod rule to estimate the integral over each subinterval. The error for each subinterval is estimated by comparison with the k-point Gauss quadrature rule. The subinterval with the largest estimated error is then bisected, and the same procedure is applied to both halves. The bisection process is continued until either the error criterion is satisfied, roundoff error is detected, the subintervals become too small, or the maximum number of subintervals allowed is reached. The function imsl_f_int_fcn is based on the subroutine QAG by Piessens et al. (1983).
On some platforms, imsl_f_int_fcn can evaluate the user-supplied function fcn in parallel. This is done only if the function imsl_omp_options is called to flag user-defined functions as thread-safe. A function is thread-safe if there are no dependencies between calls. Such dependencies are usually the result of writing to global or static variables.
Should imsl_f_int_fcn fail to produce acceptable results, consider one of the more specialized functions documented in this chaptersection.
Examples
Example 1
The value of
is computed. Since the integrand is not oscillatory, all of the default values are used. The values of the actual and estimated error are machine dependent.
 
#include <imsl.h>
#include <stdio.h>
#include <math.h>
 
float fcn(float x);
float q;
float exact;
 
int main()
{
imsl_omp_options(
IMSL_SET_FUNCTIONS_THREAD_SAFE, 1,
0);
/* evaluate the integral */
q = imsl_f_int_fcn (fcn, 0.0, 2.0, 0);
 
/* print the result and the exact answer */
exact = exp(2.0) + 1.0;
printf("integral = %10.3f\nexact = %10.3f\n", q, exact);
}
 
float fcn(float x)
{
float y;
 
y = x * (exp(x));
return y;
}
Output
 
integral = 8.389
exact = 8.389
Example 2
The value of
is computed. Since the integrand is oscillatory, rule = 6 is used. The exact value is 0.50406706. The values of the actual and estimated error are machine dependent.
 
#include <imsl.h>
#include <stdio.h>
#include <math.h>
 
float fcn(float x);
 
int main()
{
float q, err_est, err_abs= 0.0001, exact = 0.50406706, error;
 
imsl_omp_options(
IMSL_SET_FUNCTIONS_THREAD_SAFE, 1,
0);
 
/* intergrate fcn(x) from 0 to 1 */
q = imsl_f_int_fcn (fcn, 0.0, 1.0,
IMSL_ERR_ABS, err_abs,/* set abs error value*/
IMSL_RULE, 6,
IMSL_ERR_EST, &err_est, /* pass in address */
0);
 
error = q - exact;
/* print the result and the exact answer */
printf(" integral = %10.3f\n exact = %10.3f\n error = %10.3f\n ",
q, exact , error);
printf(" err_est = %g\n", err_est);
}
 
float fcn(float x)
{
/* compute sin(1/x), avoiding division by zero */
return ((x)>1.0e-5) ? sin(1.0/(x)) : 0.0;
}
Output
 
integral = 0.504
exact = 0.504
error = 0.000
err_est = 0.000170593
Warning Errors
IMSL_ROUNDOFF_CONTAMINATION
Roundoff error has been detected. The requested tolerances, “err_abs” = # and “err_rel” cannot be reached.
IMSL_PRECISION_DEGRADATION
Precision is degraded due to too fine a subdivision relative to the requested tolerance. This may be due to bad integrand behavior in the interval (#,#). Higher precision may alleviate this problem.
Fatal Errors
IMSL_MAX_SUBINTERVALS
The maximum number of subintervals allowed “max_sub” has been reached.
IMSL_STOP_USER_FCN
Request from user supplied function to stop algorithm. User flag = "#".