TheAlgorithms/C++ 1.0.0
All the 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>
Go to the source code of this file.
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. | |
double | numerical_methods::midpoint_rule::f (double x) |
A function f(x) that will be used to test the method. | |
double | numerical_methods::midpoint_rule::g (double x) |
A function g(x) that will be used to test the method. | |
double | numerical_methods::midpoint_rule::k (double x) |
A function k(x) that will be used to test the method. | |
double | numerical_methods::midpoint_rule::l (double x) |
A function l(x) that will be used to test the method. | |
static void | test (std::int32_t N, double h, double a, double b, bool used_argv_parameters) |
Self-test implementations. | |
int | main (int argc, char **argv) |
Main function. | |
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.
Definition in file midpoint_integral_method.cpp.
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 |
Definition at line 90 of file midpoint_integral_method.cpp.
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 |
Definition at line 97 of file midpoint_integral_method.cpp.
double numerical_methods::midpoint_rule::k | ( | double | x | ) |
A function k(x) that will be used to test the method.
x | The independent variable xi |
Definition at line 103 of file midpoint_integral_method.cpp.
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 |
Definition at line 109 of file midpoint_integral_method.cpp.
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
Definition at line 162 of file midpoint_integral_method.cpp.
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 |
Definition at line 52 of file midpoint_integral_method.cpp.
|
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 |
Definition at line 123 of file midpoint_integral_method.cpp.