Algorithms and Subroutines for Solving Differential Equations
Routine Name: explicit_euler
Author: Philip Nelson
Language: C++
explicit_euler
is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value. It is the most basic explicit method for numerical integration of ordinary differential equations and is the simplest Runge–Kutta method. 1
explicit_euler(T x0, T y0, T x, T dt, F f)
requires:
T x0
- the initial x
T y0
- the initial y
T x
- the value of x
for which you want to find value of y
T dt
- the delta t stepF f
- the function defining \(\frac{dy}{dx}\)The value of y
at x
.
template <typename T, typename F>
T explicit_euler(T x0, T y0, T x, T dt, F f)
{
auto tol = maceps<T>().maceps;
while (std::abs(x - x0) > tol)
{
y0 = y0 + (dt * f(x0, y0));
x0 += dt;
}
return y0;
}
int main()
{
std::cout <<
explicit_euler(0.0, -1.0, 0.4, 0.1, [](double a, double b){return a*a+2*b;})
<< std::endl;
}
-2.05836
Last Modification date: 21 March 2018