mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
All changes have been applied
This commit is contained in:
parent
7af9b8fd58
commit
6617e060f1
@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* @brief A numerical method for easy [approximation of
|
* @brief A numerical method for easy [approximation of
|
||||||
* integrals](https://en.wikipedia.org/wiki/Midpoint_method) \details The idea
|
* integrals](https://en.wikipedia.org/wiki/Midpoint_method)
|
||||||
* is to split the interval into N of intervals and use as interpolation points
|
* @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
|
* 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
|
* h = (b-a)/N where a and b are the first and last points of the interval of
|
||||||
* the integration [a, b].
|
* the integration [a, b].
|
||||||
@ -20,7 +20,6 @@
|
|||||||
*/
|
*/
|
||||||
#include <cassert> /// for assert
|
#include <cassert> /// for assert
|
||||||
#include <cmath> /// for math functions
|
#include <cmath> /// for math functions
|
||||||
#include <cmath>
|
|
||||||
#include <cstdlib> /// for std::atof
|
#include <cstdlib> /// for std::atof
|
||||||
#include <functional> /// for std::function
|
#include <functional> /// for std::function
|
||||||
#include <iostream> /// for IO operations
|
#include <iostream> /// for IO operations
|
||||||
@ -33,7 +32,7 @@
|
|||||||
namespace numerical_methods {
|
namespace numerical_methods {
|
||||||
/**
|
/**
|
||||||
* @namespace midpoint_rule
|
* @namespace midpoint_rule
|
||||||
* \brief Contains the function of the midpoint method implementation
|
* @brief Contains the function of the midpoint method implementation
|
||||||
*/
|
*/
|
||||||
namespace midpoint_rule {
|
namespace midpoint_rule {
|
||||||
/*!
|
/*!
|
||||||
@ -48,7 +47,7 @@ namespace midpoint_rule {
|
|||||||
double midpoint(const int N, const double h, const double a,
|
double midpoint(const int N, const double h, const double a,
|
||||||
const std::function<double(double)>& func) {
|
const std::function<double(double)>& func) {
|
||||||
std::map<int, double>
|
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
|
double xi = a; // Initialize xi to the starting point x0 = a
|
||||||
|
|
||||||
// Create the data table
|
// Create the data table
|
||||||
@ -77,11 +76,8 @@ double midpoint(const int N, const double h, const double a,
|
|||||||
return evaluate_integral;
|
return evaluate_integral;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace midpoint_rule
|
|
||||||
} // namespace numerical_methods
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief A function f(x) that will be used to test the method
|
* @brief A function f(x) that will be used to test the method
|
||||||
* @param x The independent variable xi
|
* @param x The independent variable xi
|
||||||
* @returns the value of the dependent variable yi = f(xi)
|
* @returns the value of the dependent variable yi = f(xi)
|
||||||
*/
|
*/
|
||||||
@ -93,8 +89,11 @@ double k(double x) { return std::sqrt(2 * std::pow(x, 3) + 3); }
|
|||||||
/** @brief Another test function */
|
/** @brief Another test function */
|
||||||
double l(double x) { return x + std::log(2 * x + 1); }
|
double l(double x) { return x + std::log(2 * x + 1); }
|
||||||
|
|
||||||
|
} // namespace midpoint_rule
|
||||||
|
} // namespace numerical_methods
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Self-test implementations
|
* @brief Self-test implementations
|
||||||
* @param N is the number of intervals
|
* @param N is the number of intervals
|
||||||
* @param h is the step
|
* @param h is the step
|
||||||
* @param a is x0
|
* @param a is x0
|
||||||
@ -106,25 +105,25 @@ static void test(int N, double h, double a, double b,
|
|||||||
bool used_argv_parameters) {
|
bool used_argv_parameters) {
|
||||||
// Call midpoint() for each of the test functions f, g, k, l
|
// Call midpoint() for each of the test functions f, g, k, l
|
||||||
// Assert with two decimal point precision
|
// Assert with two decimal point precision
|
||||||
double result_f = numerical_methods::midpoint_rule::midpoint(N, h, a, 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)) &&
|
assert((used_argv_parameters || (result_f >= 4.09 && result_f <= 4.10)) &&
|
||||||
"The result of f(x) is wrong");
|
"The result of f(x) is wrong");
|
||||||
std::cout << "The result of integral f(x) on interval [" << a << ", " << b
|
std::cout << "The result of integral f(x) on interval [" << a << ", " << b
|
||||||
<< "] is equal to: " << result_f << std::endl;
|
<< "] is equal to: " << result_f << std::endl;
|
||||||
|
|
||||||
double result_g = numerical_methods::midpoint_rule::midpoint(N, h, a, 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)) &&
|
assert((used_argv_parameters || (result_g >= 0.27 && result_g <= 0.28)) &&
|
||||||
"The result of g(x) is wrong");
|
"The result of g(x) is wrong");
|
||||||
std::cout << "The result of integral g(x) on interval [" << a << ", " << b
|
std::cout << "The result of integral g(x) on interval [" << a << ", " << b
|
||||||
<< "] is equal to: " << result_g << std::endl;
|
<< "] is equal to: " << result_g << std::endl;
|
||||||
|
|
||||||
double result_k = numerical_methods::midpoint_rule::midpoint(N, h, a, 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)) &&
|
assert((used_argv_parameters || (result_k >= 9.06 && result_k <= 9.07)) &&
|
||||||
"The result of k(x) is wrong");
|
"The result of k(x) is wrong");
|
||||||
std::cout << "The result of integral k(x) on interval [" << a << ", " << b
|
std::cout << "The result of integral k(x) on interval [" << a << ", " << b
|
||||||
<< "] is equal to: " << result_k << std::endl;
|
<< "] is equal to: " << result_k << std::endl;
|
||||||
|
|
||||||
double result_l = numerical_methods::midpoint_rule::midpoint(N, h, a, 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)) &&
|
assert((used_argv_parameters || (result_l >= 7.16 && result_l <= 7.17)) &&
|
||||||
"The result of l(x) is wrong");
|
"The result of l(x) is wrong");
|
||||||
std::cout << "The result of integral l(x) on interval [" << a << ", " << b
|
std::cout << "The result of integral l(x) on interval [" << a << ", " << b
|
||||||
@ -139,14 +138,14 @@ static void test(int N, double h, double a, double b,
|
|||||||
*/
|
*/
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
int N = 16; /// Number of intervals to divide the integration interval.
|
int N = 16; /// Number of intervals to divide the integration interval.
|
||||||
/// MUST BE EVEN
|
/// MUST BE EVEN
|
||||||
double a = 1, b = 3; /// Starting and ending point of the integration in
|
double a = 1, b = 3; /// Starting and ending point of the integration in
|
||||||
/// the real axis
|
/// 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 =
|
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 tst cases
|
// for the test cases
|
||||||
|
|
||||||
// Get user input (by the command line parameters or the console after
|
// Get user input (by the command line parameters or the console after
|
||||||
// displaying messages)
|
// displaying messages)
|
||||||
@ -170,7 +169,7 @@ int main(int argc, char** argv) {
|
|||||||
// Find the step
|
// Find the step
|
||||||
h = (b - a) / N;
|
h = (b - a) / N;
|
||||||
|
|
||||||
test(N, h, a, b, used_argv_parameters); /// run self-test implementations
|
test(N, h, a, b, used_argv_parameters); // run self-test implementations
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user