rungekutta.cpp File Reference

Runge Kutta fourth order method implementation More...

`#include <cassert>`
`#include <iostream>`
`#include <vector>`
Include dependency graph for rungekutta.cpp:

## Namespaces

| numerical_methods
| for io operations
| runge_kutta
| Functions for Runge Kutta fourth order method.

## Functions

double | numerical_methods::runge_kutta::rungeKutta (double init_x, const double &init_y, const double &x, const double &h)
| 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 More...
static double | change (double x, double y)
| for using the vector container More...
static void | test ()
| Tests to check algorithm implementation. More...
int | main ()
| Main function. More...

## Detailed Description

Runge Kutta fourth order method implementation

Author
  Rudra Prasad Das

It solves the unknown value of y for a given value of x only first order differential equations can be solved

## Function Documentation

## change()

static double change | ( | double | x,
|  | double | y
| ) |  |

static

for using the vector container

asserting the test functions for io operations

The change() function is used to return the updated iterative value corresponding to the given function

Parameters

  x | is the value corresponding to the x coordinate
  y | is the value corresponding to the y coordinate

Returns
  the computed function value at that call

Examples
  /Users/runner/work/C-Plus-Plus/C-Plus-Plus/numerical_methods/rungekutta.cpp.

{ return ((x - y) / 2.0); }

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // Execute the tests
return 0;
}
test
static void test()
Tests to check algorithm implementation.
Definition: rungekutta.cpp:100
Here is the call graph for this function:

## rungeKutta()

double numerical_methods::runge_kutta::rungeKutta | ( | double | init_x,
|  | const double & | init_y,
|  | const double & | x,
|  | const double & | h
| ) |  |

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

Parameters

  init_x | is the value of initial x and is updated after each call
  init_y | is the value of initial x and is updated after each call
  x | is current iteration at which the function needs to be evaluated
  h | is the step value

Returns
  the value of y at thr required value of x from the initial conditions

Examples
  /Users/runner/work/C-Plus-Plus/C-Plus-Plus/numerical_methods/rungekutta.cpp.

{
// 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;
}
h
int h(int key)
Definition: hash_search.cpp:45
change
static double change(double x, double y)
for using the vector container
Definition: rungekutta.cpp:33
std::vector< double >
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Tests to check algorithm implementation.

Returns
  void

{
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";
}
std::cout
numerical_methods::runge_kutta::rungeKutta
double rungeKutta(double init_x, const double &init_y, const double &x, const double &h)
the Runge Kutta method finds the value of integration of a function in the given limits....
Definition: rungekutta.cpp:57