|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Implementation of the Composite Simpson Rule for the approximation. More...
#include <cassert>#include <cmath>#include <cstdint>#include <cstdlib>#include <functional>#include <iostream>#include <map>Namespaces | |
| namespace | numerical_methods |
| for assert | |
| namespace | simpson_method |
| Contains the Simpson's method implementation. | |
Functions | |
| double | numerical_methods::simpson_method::evaluate_by_simpson (std::int32_t N, double h, double a, const std::function< double(double)> &func) |
| double | numerical_methods::simpson_method::f (double x) |
| A function f(x) that will be used to test the method. More... | |
| double | numerical_methods::simpson_method::g (double x) |
| Another test function. More... | |
| double | numerical_methods::simpson_method::k (double x) |
| Another test function. More... | |
| double | numerical_methods::simpson_method::l (double x) |
| Another test function. 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... | |
Implementation of the Composite Simpson Rule for the approximation.
The following is an implementation of the Composite Simpson Rule for the approximation of definite integrals. More info -> wiki: https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule
The idea is to split the interval in an EVEN number 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/3 * {f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}
That means that the first and last indexed i f(xi) are multiplied by 1, the odd indexed f(xi) by 4 and the even by 2.
In this program there are 4 sample test functions f, g, k, l that are evaluated in the same interval.
Arguments can be passed as parameters from the command line argv[1] = N, argv[2] = a, argv[3] = b
N must be even number and a<b.
In the end of the main() i compare the program's result with the one from mathematical software with 2 decimal points margin.
Add sample function by replacing one of the f, g, k, l and the assert
| double numerical_methods::simpson_method::evaluate_by_simpson | ( | std::int32_t | N, |
| double | h, | ||
| double | a, | ||
| const std::function< double(double)> & | func | ||
| ) |
| double numerical_methods::simpson_method::f | ( | double | x | ) |
| double numerical_methods::simpson_method::g | ( | double | x | ) |
| double numerical_methods::simpson_method::k | ( | double | x | ) |
Another test function.
| double numerical_methods::simpson_method::l | ( | double | x | ) |
Another test function.
| 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
|
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 |