mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 10:35:34 +08:00
* fix: add <cstdint> to subset_sum.cpp * fix: add <cstdint> to subarray_sum.cpp * fix: add <cstdint> to wildcard_matching.cpp * fix: add <cstdint> to count_bit_flips.cpp * fix: add <cstdint> to count_of_set_bits.cpp * fix: add <cstdint> to trailing_ciphers.cpp * fix: add <cstdint> to hamming_distance.cpp * doc: include doc for hamming_distance * fix: add <cstdint> to next_higher_numebr_with_same_number_of_set_bits.cpp * fix: add <cstdint> to power_of_2.cpp * fix: add <cstdint> to set_kth_bit.cpp * fix: add <cstdint> to bit_manipulation/set_kth_bit.cpp * fix: add <cstdint> to bit_manipulation/travelling_salesman_using_bit_manipulation.cpp * fix: add <cstdint> to ciphers/base64_encoding.cpp * fix: add <cstdint> to ciphers/hill_cipher.cpp * fix: add <cstdint> to ciphers/uint128_t.hpp * fix: add <cstdint> to data_structures/dsu_path_compression.cpp * fix: add <cstdint> to data_structures/dsu_path_compression.cpp * fix add <cstdint> to datastructures/list_array>cpp * fix add <cstdint> to datastructures/queue_using_array.cpp * fix: add <cstdint> to sparse_table.cpp * fix: add <cstdint> to stack_using_list_queue.cpp * fix: add <cstdint> to treap.cpp * fix: add <cstdint> to graham_scan_functions.hpp * fix: add <cstdint> to graph/** * fix: add integral typdefs to hashing/** * fix: add <cstdint> to math/** * fix: add <cstdint> to numerical_methods/** * fix: add <cstdint> to other/** * fix: add <cstdint> to search/** * fix: add <cstdint> to sorting/** * fix: add <cstdint> to string/** * doc: remove include statement from comment * fix: make tests static Co-authored-by: David Leal <halfpacho@gmail.com> * fix: make tests static Co-authored-by: David Leal <halfpacho@gmail.com> * chore: use iwyu on backtracking/**.cpp * chore: use iwyu on bit_manip/**.cpp * chore: use iwyu on ciphers/**.cpp * chore: use iwyu on cpu_scheduling_algorithms/**.cpp * chore: use iwyu on data_structures/**.cpp * chore: use iwyu on divide_and_conquer/**.cpp * chore: use iwyu on geometry/**.cpp * chore: use iwyu on graph/**.cpp * chore: use iwyu on hashing/**.cpp * chore: use iwyu on machine_learning/**.cpp * chore: use iwyu on math/**.cpp * chore: use iwyu on numerical_methods/**.cpp * chore: use iwyu on others/**.cpp * chore: use iwyu on probablity/**.cpp * chore: use iwyu on search/**.cpp * chore: use iwyu on sorting/**.cpp * chore: use iwyu on strings/**.cpp * Revert "chore: use iwyu on strings/**.cpp" This reverts commitf2127456a8. * Revert "chore: use iwyu on sorting/**.cpp" This reverts commita290ae7ee2. * Revert "chore: use iwyu on search/**.cpp" This reverts commit19d136ae0f. * Revert "chore: use iwyu on probablity/**.cpp" This reverts commit5dd7f82a34. * Revert "chore: use iwyu on others/**.cpp" This reverts commit8a8fd42383. * Revert "chore: use iwyu on numerical_methods/**.cpp" This reverts commiteff2f44a50. * Revert "chore: use iwyu on math/**.cpp" This reverts commitc47117ca3f. * Revert "chore: use iwyu on machine_learning/**.cpp" This reverts commitc3897d3763. * Revert "chore: use iwyu on hashing/**.cpp" This reverts commit0c6611a835. * Revert "chore: use iwyu on graph/**.cpp" This reverts commitdabd6d2591. * Revert "chore: use iwyu on geometry/**.cpp" This reverts commit740bd65932. * Revert "chore: use iwyu on divide_and_conquer/**.cpp" This reverts commit16ee49e086. * Revert "chore: use iwyu on data_structures/**.cpp" This reverts commita3b719e368. * Revert "chore: use iwyu on cpu_scheduling_algorithms/**.cpp" This reverts commit24e597f7e2. * Revert "chore: use iwyu on ciphers/**.cpp" This reverts commit3d80295883. * Revert "chore: use iwyu on bit_manip/**.cpp" This reverts commit7edcb6e458. * Revert "chore: use iwyu on backtracking/**.cpp" This reverts commitf0a30d7cdb. * Update search/binary_search.cpp * Update backtracking/subarray_sum.cpp * Update backtracking/subset_sum.cpp * Update backtracking/wildcard_matching.cpp * Update bit_manipulation/count_bits_flip.cpp * Update bit_manipulation/count_of_set_bits.cpp * Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp * Update bit_manipulation/hamming_distance.cpp * Update bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp * Update bit_manipulation/power_of_2.cpp * Update others/lru_cache.cpp * Update bit_manipulation/set_kth_bit.cpp * Update bit_manipulation/travelling_salesman_using_bit_manipulation.cpp * Update ciphers/base64_encoding.cpp * Update ciphers/hill_cipher.cpp * Update ciphers/uint128_t.hpp * Update cpu_scheduling_algorithms/fcfs_scheduling.cpp * Update data_structures/dsu_path_compression.cpp * Update data_structures/dsu_union_rank.cpp * Update data_structures/list_array.cpp * Update data_structures/queue_using_array.cpp * Update data_structures/sparse_table.cpp * Update data_structures/stack_using_queue.cpp * Update data_structures/treap.cpp * Update geometry/graham_scan_functions.hpp * Update graph/bidirectional_dijkstra.cpp * Update graph/connected_components_with_dsu.cpp * Update graph/cycle_check_directed_graph.cpp * Update graph/is_graph_bipartite2.cpp * Update graph/travelling_salesman_problem.cpp * Update hashing/md5.cpp * Update hashing/sha1.cpp * Update math/n_choose_r.cpp * Update strings/z_function.cpp * Update strings/manacher_algorithm.cpp * Update sorting/wiggle_sort.cpp * Update sorting/selection_sort_recursive.cpp * Update sorting/selection_sort_iterative.cpp * Update sorting/recursive_bubble_sort.cpp * Update sorting/radix_sort2.cpp * Update sorting/dnf_sort.cpp * Update sorting/cycle_sort.cpp * Update search/sublist_search.cpp * Update search/saddleback_search.cpp * Update search/interpolation_search.cpp * Update search/floyd_cycle_detection_algo.cpp * Update search/exponential_search.cpp * Update search/exponential_search.cpp * Update math/n_bonacci.cpp * Update math/aliquot_sum.cpp * Update math/check_factorial.cpp * Update math/double_factorial.cpp * Update math/eulers_totient_function.cpp * Update math/factorial.cpp * Update math/fibonacci.cpp * Update math/fibonacci_matrix_exponentiation.cpp * Update math/fibonacci_sum.cpp * Update math/finding_number_of_digits_in_a_number.cpp * chore: remove "/// for integral typedefs" * chore: remove for integral typedefs from modular division * fix: remove comment from include * fix: add cstdint to gale shapely --------- Co-authored-by: David Leal <halfpacho@gmail.com>
134 lines
4.0 KiB
C++
134 lines
4.0 KiB
C++
/**
|
|
* @{
|
|
* \file
|
|
* \brief [Runge Kutta fourth
|
|
* order](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods) method
|
|
* implementation
|
|
*
|
|
* \author [Rudra Prasad Das](http://github.com/rudra697)
|
|
*
|
|
* \details
|
|
* It solves the unknown value of y
|
|
* for a given value of x
|
|
* only first order differential equations
|
|
* can be solved
|
|
* \example
|
|
* it solves \frac{\mathrm{d} y}{\mathrm{d} x}= \frac{\left ( x-y \right )}{2}
|
|
* given x for given initial
|
|
* conditions
|
|
* There can be many such equations
|
|
*/
|
|
#include <cassert> /// asserting the test functions
|
|
#include <cstdint>
|
|
#include <iostream> /// for io operations
|
|
#include <vector> /// for using the vector container
|
|
/**
|
|
* @brief The change() function is used
|
|
* to return the updated iterative value corresponding
|
|
* to the given function
|
|
* @param x is the value corresponding to the x coordinate
|
|
* @param y is the value corresponding to the y coordinate
|
|
* @returns the computed function value at that call
|
|
*/
|
|
static double change(double x, double y) { return ((x - y) / 2.0); }
|
|
|
|
/**
|
|
* @namespace numerical_methods
|
|
* @brief Numerical Methods
|
|
*/
|
|
namespace numerical_methods {
|
|
/**
|
|
* @namespace runge_kutta
|
|
* @brief Functions for [Runge Kutta fourth
|
|
* order](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods) method
|
|
*/
|
|
namespace runge_kutta {
|
|
/**
|
|
* @brief the Runge Kutta method finds the value of integration of a function in
|
|
* the given limits. the lower limit of integration as the initial value and the
|
|
* upper limit is the given x
|
|
* @param init_x is the value of initial x and is updated after each call
|
|
* @param init_y is the value of initial x and is updated after each call
|
|
* @param x is current iteration at which the function needs to be evaluated
|
|
* @param h is the step value
|
|
* @returns the value of y at thr required value of x from the initial
|
|
* conditions
|
|
*/
|
|
double rungeKutta(double init_x, const double &init_y, const double &x,
|
|
const double &h) {
|
|
// Count number of iterations
|
|
// using step size or
|
|
// step height h
|
|
|
|
// n calucates the number of iterations
|
|
// k1, k2, k3, k4 are the Runge Kutta variables
|
|
// used for calculation of y at each iteration
|
|
|
|
auto n = static_cast<uint64_t>((x - init_x) / h);
|
|
// used a vector container for the variables
|
|
std::vector<double> k(4, 0.0);
|
|
|
|
// Iterate for number of iterations
|
|
|
|
double y = init_y;
|
|
for (int i = 1; i <= n; ++i) {
|
|
// Apply Runge Kutta Formulas
|
|
// to find next value of y
|
|
k[0] = h * change(init_x, y);
|
|
k[1] = h * change(init_x + 0.5 * h, y + 0.5 * k[0]);
|
|
k[2] = h * change(init_x + 0.5 * h, y + 0.5 * k[1]);
|
|
k[3] = h * change(init_x + h, y + k[2]);
|
|
|
|
// Update next value of y
|
|
|
|
y += (1.0 / 6.0) * (k[0] + 2 * k[1] + 2 * k[2] + k[3]);
|
|
|
|
// Update next value of x
|
|
|
|
init_x += h;
|
|
}
|
|
|
|
return y;
|
|
}
|
|
} // namespace runge_kutta
|
|
} // namespace numerical_methods
|
|
|
|
/**
|
|
* @brief Tests to check algorithm implementation.
|
|
* @returns void
|
|
*/
|
|
static void test() {
|
|
std::cout << "The Runge Kutta function will be tested on the basis of "
|
|
"precomputed values\n";
|
|
|
|
std::cout << "Test 1...."
|
|
<< "\n";
|
|
double valfirst = numerical_methods::runge_kutta::rungeKutta(
|
|
2, 3, 4, 0.2); // Tests the function with pre calculated values
|
|
assert(valfirst == 3.10363932323749570);
|
|
std::cout << "Passed Test 1\n";
|
|
|
|
std::cout << "Test 2...."
|
|
<< "\n";
|
|
double valsec = numerical_methods::runge_kutta::rungeKutta(
|
|
1, 2, 5, 0.1); // The value of step changed
|
|
assert(valsec == 3.40600589380261409);
|
|
std::cout << "Passed Test 2\n";
|
|
|
|
std::cout << "Test 3...."
|
|
<< "\n";
|
|
double valthird = numerical_methods::runge_kutta::rungeKutta(
|
|
-1, 3, 4, 0.1); // Tested with negative value
|
|
assert(valthird == 2.49251005860244268);
|
|
std::cout << "Passed Test 3\n";
|
|
}
|
|
|
|
/**
|
|
* @brief Main function
|
|
* @returns 0 on exit
|
|
*/
|
|
int main() {
|
|
test(); // Execute the tests
|
|
return 0;
|
|
}
|