|
void | forward_euler_step (const double dx, const double x, std::valarray< double > *y, std::valarray< double > *dy) |
| Compute next step approximation using the forward-Euler method.
|
|
double | forward_euler (double dx, double x0, double x_max, std::valarray< double > *y, bool save_to_file=false) |
| Compute approximation using the forward-Euler method in the given limits.
|
|
void | midpoint_euler_step (const double dx, const double &x, std::valarray< double > *y, std::valarray< double > *dy) |
| Compute next step approximation using the midpoint-Euler method.
|
|
double | midpoint_euler (double dx, double x0, double x_max, std::valarray< double > *y, bool save_to_file=false) |
| Compute approximation using the midpoint-Euler method in the given limits.
|
|
void | semi_implicit_euler_step (const double dx, const double &x, std::valarray< double > *y, std::valarray< double > *dy) |
| Compute next step approximation using the semi-implicit-Euler method.
|
|
double | semi_implicit_euler (double dx, double x0, double x_max, std::valarray< double > *y, bool save_to_file=false) |
| Compute approximation using the semi-implicit-Euler method in the given limits.
|
|
Integration functions for implementations with solving ordinary differential equations (ODEs) of any order and and any number of independent variables.
◆ forward_euler()
double forward_euler |
( |
double | dx, |
|
|
double | x0, |
|
|
double | x_max, |
|
|
std::valarray< double > * | y, |
|
|
bool | save_to_file = false ) |
Compute approximation using the forward-Euler method in the given limits.
- Parameters
-
[in] | dx | step size |
[in] | x0 | initial value of independent variable |
[in] | x_max | final value of independent variable |
[in,out] | y | take \(y_n\) and compute \(y_{n+1}\) |
[in] | save_to_file | flag to save results to a CSV file (1) or not (0) |
- Returns
- time taken for computation in seconds
103 {
105
107 if (save_to_file) {
108 fp.
open(
"forward_euler.csv", std::ofstream::out);
111 }
112 }
113
115
116
118 double x = x0;
119
120 do {
121 if (save_to_file && fp.
is_open()) {
122
123 fp << x << ",";
124 for (int i = 0; i < L - 1; i++) {
125 fp << y[0][i] << ",";
126 }
127 fp << y[0][L - 1] << "\n";
128 }
129
131 x += dx;
132 } while (x <= x_max);
133
135
138 }
139
140 return static_cast<double>(t2 - t1) / CLOCKS_PER_SEC;
141}
void forward_euler_step(const double dx, const double x, std::valarray< double > *y, std::valarray< double > *dy)
Compute next step approximation using the forward-Euler method.
Definition ode_forward_euler.cpp:86
◆ forward_euler_step()
Compute next step approximation using the forward-Euler method.
\[y_{n+1}=y_n + dx\cdot f\left(x_n,y_n\right)\]
- Parameters
-
[in] | dx | step size |
[in] | x | take \(x_n\) and compute \(x_{n+1}\) |
[in,out] | y | take \(y_n\) and compute \(y_{n+1}\) |
[in,out] | dy | compute \(f\left(x_n,y_n\right)\) |
87 {
89 *y += *dy * dx;
90}
void problem(const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
Definition ode_forward_euler.cpp:54
◆ midpoint_euler()
double midpoint_euler |
( |
double | dx, |
|
|
double | x0, |
|
|
double | x_max, |
|
|
std::valarray< double > * | y, |
|
|
bool | save_to_file = false ) |
Compute approximation using the midpoint-Euler method in the given limits.
- Parameters
-
[in] | dx | step size |
[in] | x0 | initial value of independent variable |
[in] | x_max | final value of independent variable |
[in,out] | y | take \(y_n\) and compute \(y_{n+1}\) |
[in] | save_to_file | flag to save results to a CSV file (1) or not (0) |
- Returns
- time taken for computation in seconds
108 {
110
112 if (save_to_file) {
113 fp.
open(
"midpoint_euler.csv", std::ofstream::out);
116 }
117 }
118
120
121
123 double x = x0;
124 do {
125 if (save_to_file && fp.
is_open()) {
126
127 fp << x << ",";
128 for (int i = 0; i < L - 1; i++) {
129 fp << y[0][i] << ",";
130 }
131 fp << y[0][L - 1] << "\n";
132 }
133
135 x += dx;
136 } while (x <= x_max);
137
139
142
143 return static_cast<double>(t2 - t1) / CLOCKS_PER_SEC;
144}
void midpoint_euler_step(const double dx, const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Compute next step approximation using the midpoint-Euler method.
Definition ode_midpoint_euler.cpp:85
◆ midpoint_euler_step()
Compute next step approximation using the midpoint-Euler method.
\[y_{n+1} = y_n + dx\, f\left(x_n+\frac{1}{2}dx,
y_n + \frac{1}{2}dx\,f\left(x_n,y_n\right)\right)\]
- Parameters
-
[in] | dx | step size |
[in] | x | take \(x_n\) and compute \(x_{n+1}\) |
[in,out] | y | take \(y_n\) and compute \(y_{n+1}\) |
[in,out] | dy | compute \(f\left(x_n,y_n\right)\) |
86 {
88 double tmp_x = x + 0.5 * dx;
89
91
93
94 y[0] += dy[0] * dx;
95}
void problem(const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
Definition ode_midpoint_euler.cpp:53
◆ semi_implicit_euler()
double semi_implicit_euler |
( |
double | dx, |
|
|
double | x0, |
|
|
double | x_max, |
|
|
std::valarray< double > * | y, |
|
|
bool | save_to_file = false ) |
Compute approximation using the semi-implicit-Euler method in the given limits.
- Parameters
-
[in] | dx | step size |
[in] | x0 | initial value of independent variable |
[in] | x_max | final value of independent variable |
[in,out] | y | take \(y_n\) and compute \(y_{n+1}\) |
[in] | save_to_file | flag to save results to a CSV file (1) or not (0) |
- Returns
- time taken for computation in seconds
105 {
107
109 if (save_to_file) {
110 fp.
open(
"semi_implicit_euler.csv", std::ofstream::out);
113 }
114 }
115
117
118
120 double x = x0;
121 do {
122 if (save_to_file && fp.
is_open()) {
123
124 fp << x << ",";
125 for (int i = 0; i < L - 1; i++) {
126 fp << y[0][i] << ",";
127 }
128 fp << y[0][L - 1] << "\n";
129 }
130
132 x += dx;
133 } while (x <= x_max);
134
136
139
140 return static_cast<double>(t2 - t1) / CLOCKS_PER_SEC;
141}
void semi_implicit_euler_step(const double dx, const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Compute next step approximation using the semi-implicit-Euler method.
Definition ode_semi_implicit_euler.cpp:82
◆ semi_implicit_euler_step()
Compute next step approximation using the semi-implicit-Euler method.
\[y_{n+1}=y_n + dx\cdot f\left(x_n,y_n\right)\]
- Parameters
-
[in] | dx | step size |
[in] | x | take \(x_n\) and compute \(x_{n+1}\) |
[in,out] | y | take \(y_n\) and compute \(y_{n+1}\) |
[in,out] | dy | compute \(f\left(x_n,y_n\right)\) |
84 {
86 y[0][0] += dx * dy[0][0];
88
89 dy[0][0] = 0.f;
90 y[0] += dy[0] * dx;
91}
void problem(const double &x, std::valarray< double > *y, std::valarray< double > *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
Definition ode_semi_implicit_euler.cpp:53