Xmipp  v3.23.11-Nereus
Classes | Functions
Numerical integration
Collaboration diagram for Numerical integration:

Classes

class  doubleFunction
 
class  Trapeze
 
class  Romberg
 

Functions

double integrateNewtonCotes (double(*f)(double), double a, double b, int N)
 

Detailed Description

This code performs numeric integrations as described in the Numerical Recipes Book, in particular it implements the "trapezoidal" (Trapeze) and the Romberg integration. Both are designed for smoothly variant functions.

This module can also perform multidimensional integration.

Function Documentation

◆ integrateNewtonCotes()

double integrateNewtonCotes ( double(*)(double)  f,
double  a,
double  b,
int  N 
)

Integrate a function using Newton-Cotes formula

Estimate the integral of a function between a and b using N points.

Definition at line 32 of file integration.cpp.

34 {
35  if (N < 2 || N > 9)
36  REPORT_ERROR(ERR_VALUE_INCORRECT, "integrateNewtonCotes: N must be greater than 1");
37  double h = (b - a) / (N - 1);
38  Matrix1D<double> fx(N);
39  for (int i = 0; i < N; i++)
40  fx(i) = (*f)(a + i * h);
41  switch (N)
42  {
43  case 2:
44  return h / 2*(fx(0) + fx(1));
45  case 3:
46  return h / 3*(fx(0) + 4*fx(1) + fx(2));
47  case 4:
48  return h*3.0 / 8.0*((fx(0) + fx(3)) + 3*(fx(1) + fx(2)));
49  case 5:
50  return h*2.0 / 45.0*(7*(fx(0) + fx(4)) + 32*(fx(1) + fx(3)) + 12*fx(2));
51  case 6:
52  return h*5.0 / 288.0*(19*(fx(0) + fx(5)) + 75*(fx(1) + fx(4)) + 50*(fx(2) + fx(3)));
53  case 7:
54  return h / 140.0*(41*(fx(0) + fx(6)) + 216*(fx(1) + fx(5)) + 27*(fx(2) + fx(4)) +
55  272*fx(3));
56  case 8:
57  return h*7.0 / 17280.0*(751*(fx(0) + fx(7)) + 3577*(fx(1) + fx(6)) + 1323*(fx(2) + fx(5)) +
58  2989*(fx(3) + fx(4)));
59  case 9:
60  return 4.0 / 14175.0*h*(989*(fx(0) + fx(8)) + 5888*(fx(1) + fx(7)) +
61  -928*(fx(2) + fx(6)) + 10496*(fx(3) + fx(5)) - 4540*fx(4));
62  }
63  REPORT_ERROR(ERR_ARG_INCORRECT,"Number of points is too high");
64 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define i
doublereal * b
Incorrect argument received.
Definition: xmipp_error.h:113
Incorrect value received.
Definition: xmipp_error.h:195
doublereal * a