![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Solve a multivariable first order ordinary differential equation (ODEs) using midpoint Euler method More...
#include <cmath>#include <ctime>#include <fstream>#include <iostream>#include <valarray>Go to the source code of this file.
Functions | |
| 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 differential variables. | |
| void | exact_solution (const double &x, std::valarray< double > *y) |
| Exact solution of the problem. Used for solution comparison. | |
| 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 | save_exact_solution (const double &X0, const double &X_MAX, const double &step_size, const std::valarray< double > &Y0) |
| int | main (int argc, char *argv[]) |
Solve a multivariable first order ordinary differential equation (ODEs) using midpoint Euler method
The ODE being solved is:
\begin{eqnarray*} \dot{u} &=& v\\ \dot{v} &=& -\omega^2 u\\ \omega &=& 1\\ [x_0, u_0, v_0] &=& [0,1,0]\qquad\ldots\text{(initial values)} \end{eqnarray*}
The exact solution for the above problem is:
\begin{eqnarray*} u(x) &=& \cos(x)\\ v(x) &=& -\sin(x)\\ \end{eqnarray*}
The computation results are stored to a text file midpoint_euler.csv and the exact soltuion results in exact.csv for comparison.
To implement Van der Pol oscillator, change the problem function to:
Definition in file ode_midpoint_euler.cpp.
| void exact_solution | ( | const double & | x, |
| std::valarray< double > * | y ) |
Exact solution of the problem. Used for solution comparison.
| [in] | x | independent variable |
| [in,out] | y | dependent variable |
Definition at line 66 of file ode_midpoint_euler.cpp.
| int main | ( | int | argc, |
| char * | argv[] ) |
Main Function
Definition at line 192 of file ode_midpoint_euler.cpp.
| 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 differential variables.
| [in] | x | independent variable(s) |
| [in,out] | y | dependent variable(s) |
| [in,out] | dy | first-derivative of dependent variable(s) |
Definition at line 53 of file ode_midpoint_euler.cpp.
| void save_exact_solution | ( | const double & | X0, |
| const double & | X_MAX, | ||
| const double & | step_size, | ||
| const std::valarray< double > & | Y0 ) |
Function to compute and save exact solution for comparison
| [in] | X0 | initial value of independent variable |
| [in] | X_MAX | final value of independent variable |
| [in] | step_size | independent variable step size |
| [in] | Y0 | initial values of dependent variables |
Definition at line 156 of file ode_midpoint_euler.cpp.