In this tutorial, we know how to define a general-purpose function to compute the numerical integral using Python and Numpy package. The Python code supports the vectorized operations on input array and that is why the code is so concise and effective.
In this article, the same idea will be implemented using C++. As we notice, we need to pass the function as a parameter into the C++ function, and the first step is to use the function pointer.
1 | double(*f)(double x) |
double(*f)(double x)
The above tells the compiler that the parameter f is a pointer to function. And the pointed function takes a double input and returns a double. Therefore the mathematical functions like sin, cos, atan etc can be easily passed into the function for integral.
The function signature is defined in C++ as follows:
1 | double integral(double(*f)(double x), double a, double b, int n) |
double integral(double(*f)(double x), double a, double b, int n)
This function computes the definite integral for f(x) between range a and b inclusive. The parameter n defines how many steps we need to divide the area.
So the definite integral can be considered as the signed area between the specified range along the X-axis. The area will be approximated to the sum of each small rectangles. And therefore, if we have more rectangles, we will have a closer approximation to the integral value.
We then can write the C++ function to sum up each rectangle, we use the middle point of the rectangle to compute the f(x) for the rectangle height, which gives higher accuracy.
1 2 3 4 5 6 7 8 9 | // helloacm.com double integral(double(*f)(double x), double a, double b, int n) { double step = (b - a) / n; // width of each small rectangle double area = 0.0; // signed area for (int i = 0; i < n; i ++) { area += f(a + (i + 0.5) * step) * step; // sum up each small rectangle } return area; } |
// helloacm.com double integral(double(*f)(double x), double a, double b, int n) { double step = (b - a) / n; // width of each small rectangle double area = 0.0; // signed area for (int i = 0; i < n; i ++) { area += f(a + (i + 0.5) * step) * step; // sum up each small rectangle } return area; }
To invoke this, simply call:
1 2 3 4 5 6 | int main() { cout.precision(7); cout << integral(cos, 0, M_PI / 2, 10); return 0; } |
int main() { cout.precision(7); cout << integral(cos, 0, M_PI / 2, 10); return 0; }
This will produce value 1.001029 and if we split into 100 rectangles, we will have a higher accuracy: 1.00001.
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: How to Compute the mean of a distribution using Python and Numpy?
Next Post: C++ Function Pointers to Member Methods in Class
#include
#include
#include
using myFunc = std::function;
//
// definite integral for: interval a,b (with n steps)
//
double integral(myFunc f, double a, double b, int n = 100) {
const double step = (b – a) / n; // width of each small rectangle
double area = 0.0; // signed area
for (int i = 0; i < n; ++i) {
const double & width = step;
const double & height = f(a + (i + 0.5) * step);
area += width * height; // sum up each small rectangle
}
return area;
}
int main()
{
myFunc f =
[](double x) { return cos(x); } // our f(x)
;
std::cout.precision(7);
std::cout << integral(f, 0, M_PI / 2, 10);
return 0;
}