mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-11 06:26:05 +08:00
clang-format and clang-tidy fixes for 6617e060
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief A numerical method for easy [approximation of
|
||||
* integrals](https://en.wikipedia.org/wiki/Midpoint_method)
|
||||
* @details 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].
|
||||
* integrals](https://en.wikipedia.org/wiki/Midpoint_method)
|
||||
* @details 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) + ... +
|
||||
@@ -18,8 +18,8 @@
|
||||
*
|
||||
* @author [ggkogkou](https://github.com/ggkogkou)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for math functions
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for math functions
|
||||
#include <cstdlib> /// for std::atof
|
||||
#include <functional> /// for std::function
|
||||
#include <iostream> /// for IO operations
|
||||
@@ -47,7 +47,7 @@ namespace midpoint_rule {
|
||||
double midpoint(const int N, const double h, const double a,
|
||||
const std::function<double(double)>& func) {
|
||||
std::map<int, double>
|
||||
data_table; // Contains the data points, key: i, value: f(xi)
|
||||
data_table; // Contains the data points, key: i, value: f(xi)
|
||||
double xi = a; // Initialize xi to the starting point x0 = a
|
||||
|
||||
// Create the data table
|
||||
@@ -105,25 +105,29 @@ static void test(int N, double h, double a, double b,
|
||||
bool used_argv_parameters) {
|
||||
// Call midpoint() for each of the test functions f, g, k, l
|
||||
// Assert with two decimal point precision
|
||||
double result_f = numerical_methods::midpoint_rule::midpoint(N, h, a, numerical_methods::midpoint_rule::f);
|
||||
double result_f = numerical_methods::midpoint_rule::midpoint(
|
||||
N, h, a, numerical_methods::midpoint_rule::f);
|
||||
assert((used_argv_parameters || (result_f >= 4.09 && result_f <= 4.10)) &&
|
||||
"The result of f(x) is wrong");
|
||||
std::cout << "The result of integral f(x) on interval [" << a << ", " << b
|
||||
<< "] is equal to: " << result_f << std::endl;
|
||||
|
||||
double result_g = numerical_methods::midpoint_rule::midpoint(N, h, a, numerical_methods::midpoint_rule::g);
|
||||
double result_g = numerical_methods::midpoint_rule::midpoint(
|
||||
N, h, a, numerical_methods::midpoint_rule::g);
|
||||
assert((used_argv_parameters || (result_g >= 0.27 && result_g <= 0.28)) &&
|
||||
"The result of g(x) is wrong");
|
||||
std::cout << "The result of integral g(x) on interval [" << a << ", " << b
|
||||
<< "] is equal to: " << result_g << std::endl;
|
||||
|
||||
double result_k = numerical_methods::midpoint_rule::midpoint(N, h, a, numerical_methods::midpoint_rule::k);
|
||||
double result_k = numerical_methods::midpoint_rule::midpoint(
|
||||
N, h, a, numerical_methods::midpoint_rule::k);
|
||||
assert((used_argv_parameters || (result_k >= 9.06 && result_k <= 9.07)) &&
|
||||
"The result of k(x) is wrong");
|
||||
std::cout << "The result of integral k(x) on interval [" << a << ", " << b
|
||||
<< "] is equal to: " << result_k << std::endl;
|
||||
|
||||
double result_l = numerical_methods::midpoint_rule::midpoint(N, h, a, numerical_methods::midpoint_rule::l);
|
||||
double result_l = numerical_methods::midpoint_rule::midpoint(
|
||||
N, h, a, numerical_methods::midpoint_rule::l);
|
||||
assert((used_argv_parameters || (result_l >= 7.16 && result_l <= 7.17)) &&
|
||||
"The result of l(x) is wrong");
|
||||
std::cout << "The result of integral l(x) on interval [" << a << ", " << b
|
||||
@@ -141,10 +145,10 @@ int main(int argc, char** argv) {
|
||||
/// MUST BE EVEN
|
||||
double a = 1, b = 3; /// Starting and ending point of the integration in
|
||||
/// the real axis
|
||||
double h = NAN; /// Step, calculated by a, b and N
|
||||
double h = NAN; /// Step, calculated by a, b and N
|
||||
|
||||
bool used_argv_parameters =
|
||||
false; // If argv parameters are used then the assert must be omitted
|
||||
false; // If argv parameters are used then the assert must be omitted
|
||||
// for the test cases
|
||||
|
||||
// Get user input (by the command line parameters or the console after
|
||||
|
||||
Reference in New Issue
Block a user