|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
A numerical method for easy approximation of integrals More...
#include <cassert>#include <cmath>#include <cstdint>#include <cstdlib>#include <functional>#include <iostream>#include <map>Namespaces | |
| namespace | numerical_methods |
| for assert | |
| namespace | midpoint_rule |
| Functions for the Midpoint Integral method implementation. | |
Functions | |
| double | numerical_methods::midpoint_rule::midpoint (const std::int32_t N, const double h, const double a, const std::function< double(double)> &func) |
| Main function for implementing the Midpoint Integral Method implementation. More... | |
| double | numerical_methods::midpoint_rule::f (double x) |
| A function f(x) that will be used to test the method. More... | |
| double | numerical_methods::midpoint_rule::g (double x) |
| A function g(x) that will be used to test the method. More... | |
| double | numerical_methods::midpoint_rule::k (double x) |
| A function k(x) that will be used to test the method. More... | |
| double | numerical_methods::midpoint_rule::l (double x) |
| A function l(x) that will be used to test the method. More... | |
| static void | test (std::int32_t N, double h, double a, double b, bool used_argv_parameters) |
| Self-test implementations. More... | |
| int | main (int argc, char **argv) |
| Main function. More... | |
A numerical method for easy approximation of integrals
The idea is to split the interval into N of intervals and use as interpolation points the xi for which it applies that xi = x0 + i*h, where h is a step defined as h = (b-a)/N where a and b are the first and last points of the interval of the integration [a, b].
We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula: I = h * {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}
Arguments can be passed as parameters from the command line argv[1] = N, argv[2] = a, argv[3] = b. In this case if the default values N=16, a=1, b=3 are changed then the tests/assert are disabled.
| double numerical_methods::midpoint_rule::f | ( | double | x | ) |
A function f(x) that will be used to test the method.
| x | The independent variable xi |
| double numerical_methods::midpoint_rule::g | ( | double | x | ) |
A function g(x) that will be used to test the method.
| x | The independent variable xi |
| double numerical_methods::midpoint_rule::k | ( | double | x | ) |
| double numerical_methods::midpoint_rule::l | ( | double | x | ) |
A function l(x) that will be used to test the method.
| x | The independent variable xi |
| int main | ( | int | argc, |
| char ** | argv | ||
| ) |
Main function.
| argc | commandline argument count (ignored) |
| argv | commandline array of arguments (ignored) |
Number of intervals to divide the integration interval.
MUST BE EVEN
Starting and ending point of the integration in
the real axis
Step, calculated by a, b and N
| double numerical_methods::midpoint_rule::midpoint | ( | const std::int32_t | N, |
| const double | h, | ||
| const double | a, | ||
| const std::function< double(double)> & | func | ||
| ) |
Main function for implementing the Midpoint Integral Method implementation.
| N | is the number of intervals |
| h | is the step |
| a | is x0 |
| func | is the function that will be integrated |
|
static |
Self-test implementations.
| N | is the number of intervals |
| h | is the step |
| a | is x0 |
| b | is the end of the interval |
| used_argv_parameters | is 'true' if argv parameters are given and 'false' if not |