TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
midpoint_integral_method.cpp File Reference

A numerical method for easy approximation of integrals More...

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
Include dependency graph for midpoint_integral_method.cpp:

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.
 

Detailed Description

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.

Author
ggkogkou

Definition in file midpoint_integral_method.cpp.

Function Documentation

◆ f()

double numerical_methods::midpoint_rule::f ( double x)

A function f(x) that will be used to test the method.

Parameters
xThe independent variable xi
Returns
the value of the dependent variable yi = f(xi) = sqrt(xi) + ln(xi)

Definition at line 90 of file midpoint_integral_method.cpp.

90{ return std::sqrt(x) + std::log(x); }

◆ g()

double numerical_methods::midpoint_rule::g ( double x)

A function g(x) that will be used to test the method.

Parameters
xThe independent variable xi
Returns
the value of the dependent variable yi = g(xi) = e^(-xi) * (4 - xi^2)

Definition at line 97 of file midpoint_integral_method.cpp.

97{ return std::exp(-x) * (4 - std::pow(x, 2)); }

◆ k()

double numerical_methods::midpoint_rule::k ( double x)

A function k(x) that will be used to test the method.

Parameters
xThe independent variable xi
Returns
the value of the dependent variable yi = k(xi) = sqrt(2*xi^3 + 3)

Definition at line 103 of file midpoint_integral_method.cpp.

103{ return std::sqrt(2 * std::pow(x, 3) + 3); }

◆ l()

double numerical_methods::midpoint_rule::l ( double x)

A function l(x) that will be used to test the method.

Parameters
xThe independent variable xi
Returns
the value of the dependent variable yi = l(xi) = xi + ln(2*xi + 1)

Definition at line 109 of file midpoint_integral_method.cpp.

109{ return x + std::log(2 * x + 1); }

◆ main()

int main ( int argc,
char ** argv )

Main function.

Parameters
argccommandline argument count (ignored)
argvcommandline array of arguments (ignored)
Returns
0 on exit

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.

162 {
163 std::int32_t N =
164 16;
166 double a = 1, b = 3;
168 double h = NAN;
169
170 bool used_argv_parameters =
171 false; // If argv parameters are used then the assert must be omitted
172 // for the test cases
173
174 // Get user input (by the command line parameters or the console after
175 // displaying messages)
176 if (argc == 4) {
177 N = std::atoi(argv[1]);
178 a = std::atof(argv[2]);
179 b = std::atof(argv[3]);
180 // Check if a<b else abort
181 assert(a < b && "a has to be less than b");
182 assert(N > 0 && "N has to be > 0");
183 if (N < 4 || a != 1 || b != 3) {
184 used_argv_parameters = true;
185 }
186 std::cout << "You selected N=" << N << ", a=" << a << ", b=" << b
187 << std::endl;
188 } else {
189 std::cout << "Default N=" << N << ", a=" << a << ", b=" << b
190 << std::endl;
191 }
192
193 // Find the step
194 h = (b - a) / N;
195
196 test(N, h, a, b, used_argv_parameters); // run self-test implementations
197
198 return 0;
199}
static void test()
Self-test implementations.
int h(int key)
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....

◆ midpoint()

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.

Parameters
Nis the number of intervals
his the step
ais x0
funcis the function that will be integrated
Returns
the result of the integration

Definition at line 52 of file midpoint_integral_method.cpp.

53 {
54 std::map<int, double>
55 data_table; // Contains the data points, key: i, value: f(xi)
56 double xi = a; // Initialize xi to the starting point x0 = a
57
58 // Create the data table
59 // Loop from x0 to xN-1
60 double temp = NAN;
61 for (std::int32_t i = 0; i < N; i++) {
62 temp = func(xi + h / 2); // find f(xi+h/2)
63 data_table.insert(
64 std::pair<std::int32_t, double>(i, temp)); // add i and f(xi)
65 xi += h; // Get the next point xi for the next iteration
66 }
67
68 // Evaluate the integral.
69 // Remember: {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}
70 double evaluate_integral = 0;
71 for (std::int32_t i = 0; i < N; i++) evaluate_integral += data_table.at(i);
72
73 // Multiply by the coefficient h
74 evaluate_integral *= h;
75
76 // If the result calculated is nan, then the user has given wrong input
77 // interval.
78 assert(!std::isnan(evaluate_integral) &&
79 "The definite integral can't be evaluated. Check the validity of "
80 "your input.\n");
81 // Else return
82 return evaluate_integral;
83}

◆ test()

static void test ( std::int32_t N,
double h,
double a,
double b,
bool used_argv_parameters )
static

Self-test implementations.

Parameters
Nis the number of intervals
his the step
ais x0
bis the end of the interval
used_argv_parametersis 'true' if argv parameters are given and 'false' if not

Definition at line 123 of file midpoint_integral_method.cpp.

124 {
125 // Call midpoint() for each of the test functions f, g, k, l
126 // Assert with two decimal point precision
128 N, h, a, numerical_methods::midpoint_rule::f);
129 assert((used_argv_parameters || (result_f >= 4.09 && result_f <= 4.10)) &&
130 "The result of f(x) is wrong");
131 std::cout << "The result of integral f(x) on interval [" << a << ", " << b
132 << "] is equal to: " << result_f << std::endl;
133
135 N, h, a, numerical_methods::midpoint_rule::g);
136 assert((used_argv_parameters || (result_g >= 0.27 && result_g <= 0.28)) &&
137 "The result of g(x) is wrong");
138 std::cout << "The result of integral g(x) on interval [" << a << ", " << b
139 << "] is equal to: " << result_g << std::endl;
140
142 N, h, a, numerical_methods::midpoint_rule::k);
143 assert((used_argv_parameters || (result_k >= 9.06 && result_k <= 9.07)) &&
144 "The result of k(x) is wrong");
145 std::cout << "The result of integral k(x) on interval [" << a << ", " << b
146 << "] is equal to: " << result_k << std::endl;
147
149 N, h, a, numerical_methods::midpoint_rule::l);
150 assert((used_argv_parameters || (result_l >= 7.16 && result_l <= 7.17)) &&
151 "The result of l(x) is wrong");
152 std::cout << "The result of integral l(x) on interval [" << a << ", " << b
153 << "] is equal to: " << result_l << std::endl;
154}
double 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.