A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d1/da6/rungekutta_8cpp.html below:

TheAlgorithms/C++: numerical_methods/rungekutta.cpp File Reference

Loading...

Searching...

No Matches

Runge Kutta fourth order method implementation More...

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>

Go to the source code of this file.

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
static double  change (double x, double y)   asserting the test functions
static void  test ()   Tests to check algorithm implementation.
int  main ()   Main function.

Runge Kutta fourth order method implementation

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

Definition in file rungekutta.cpp.

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

asserting the test functions

for io operations for using the vector container

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.

Definition at line 33 of file rungekutta.cpp.

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

◆ main()

Main function.

Returns
0 on exit

Definition at line 130 of file rungekutta.cpp.

130 {

132 return 0;

133}

static void test()

Tests to check algorithm implementation.

◆ 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.

Definition at line 57 of file rungekutta.cpp.

58 {

59

60

61

62

63

64

65

66

67 auto

n =

static_cast<

uint64_t

>

((x - init_x) /

h

);

68

69 std::vector<double> k(4, 0.0);

70

71

72

73 double y = init_y;

74 for (int i = 1; i <= n; ++i) {

75

76

78

k[1] =

h

*

change

(init_x + 0.5 *

h

, y + 0.5 * k[0]);

79

k[2] =

h

*

change

(init_x + 0.5 *

h

, y + 0.5 * k[1]);

80

k[3] =

h

*

change

(init_x +

h

, y + k[2]);

81

82

83

84 y += (1.0 / 6.0) * (k[0] + 2 * k[1] + 2 * k[2] + k[3]);

85

86

87

89 }

90

91 return y;

92}

static double change(double x, double y)

asserting the test functions

◆ test()

Tests to check algorithm implementation.

Returns
void

Definition at line 100 of file rungekutta.cpp.

100 {

101 std::cout << "The Runge Kutta function will be tested on the basis of "

102 "precomputed values\n";

103

104 std::cout << "Test 1...."

105 << "\n";

107 2, 3, 4, 0.2);

108 assert(valfirst == 3.10363932323749570);

109 std::cout << "Passed Test 1\n";

110

111 std::cout << "Test 2...."

112 << "\n";

114 1, 2, 5, 0.1);

115 assert(valsec == 3.40600589380261409);

116 std::cout << "Passed Test 2\n";

117

118 std::cout << "Test 3...."

119 << "\n";

121 -1, 3, 4, 0.1);

122 assert(valthird == 2.49251005860244268);

123 std::cout << "Passed Test 3\n";

124}

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....


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4