From 71722d5884acbc94f96254a295fd987d2730a85d Mon Sep 17 00:00:00 2001 From: ggkogkou Date: Sat, 23 Oct 2021 15:38:42 +0300 Subject: [PATCH] added namespace numerical_methods --- .../midpoint_integral_method.cpp | 93 ++++++++++--------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/numerical_methods/midpoint_integral_method.cpp b/numerical_methods/midpoint_integral_method.cpp index a961da575..441d8bad5 100644 --- a/numerical_methods/midpoint_integral_method.cpp +++ b/numerical_methods/midpoint_integral_method.cpp @@ -2,8 +2,8 @@ #include /// for math functions #include /// for assert #include /// for std::atof -#include /// forstd::function -#include /// for std::map +#include /// for std::function +#include /// for std::map container /*! * @file @@ -23,48 +23,55 @@ * @author [ggkogkou](https://github.com/ggkogkou) */ +/** + * @namespace numerical_methods + * @brief Numerical algorithms/methods + */ +namespace numerical_methods { /** * @namespace midpoint_rule * \brief Contains the function of the midpoint method implementation */ -namespace midpoint_rule{ - /*! - * @fn double midpoint(const int N, const double h, const double a, const std::function& func) - * \brief Implement midpoint method - * @param N is the number of intervals - * @param h is the step - * @param a is x0 - * @param func is the function that will be integrated - * @returns the result of the integration - */ - double midpoint(const int N, const double h, const double a, const std::function& func){ - std::map data_table; // Contains the data points, key: i, value: f(xi) - double xi = a; // Initialize xi to the starting point x0 = a + namespace midpoint_rule { + /*! + * @fn double midpoint(const int N, const double h, const double a, const std::function& func) + * \brief Implement midpoint method + * @param N is the number of intervals + * @param h is the step + * @param a is x0 + * @param func is the function that will be integrated + * @returns the result of the integration + */ + double midpoint(const int N, const double h, const double a, const std::function &func) { + std::map 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 - // Loop from x0 to xN-1 - double temp; - for(int i=0; i(i, temp)); // add i and f(xi) - xi += h; // Get the next point xi for the next iteration + // Create the data table + // Loop from x0 to xN-1 + double temp; + for (int i = 0; i < N; i++) { + temp = func(xi + h / 2); // find f(xi+h/2) + data_table.insert(std::pair(i, temp)); // add i and f(xi) + xi += h; // Get the next point xi for the next iteration + } + + // Evaluate the integral. + // Remember: {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)} + double evaluate_integral = 0; + for (int i = 0; i < N; i++) evaluate_integral += data_table.at(i); + + // Multiply by the coefficient h + evaluate_integral *= h; + + // If the result calculated is nan, then the user has given wrong input interval. + assert(!std::isnan(evaluate_integral) && + "The definite integral can't be evaluated. Check the validity of your input.\n"); + // Else return + return evaluate_integral; } - // Evaluate the integral. - // Remember: {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)} - double evaluate_integral = 0; - for(int i=0; i= 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 = midpoint_rule::midpoint(N, h, a, g); + double result_g = numerical_methods::midpoint_rule::midpoint(N, h, a, 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 = midpoint_rule::midpoint(N, h, a, k); + double result_k = numerical_methods::midpoint_rule::midpoint(N, h, a, 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 = midpoint_rule::midpoint(N, h, a, l); + double result_l = numerical_methods::midpoint_rule::midpoint(N, h, a, 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 << "] is equal to: " << result_l << std::endl;