FUNCTION
euler
Computes an approximate solution to the Cauchy problem using Euler's method.
Interface
#include <codecogs/maths/calculus/ode/euler.h>
using namespace Maths::Calculus::Ode;
Consider a continuous function, where
is a real interval and let
be the initial value in the Cauchy problem:
We need to find the function which satisfies the above conditions. It may happen that one cannot be able to compute the solution analytically, and in those cases numerical methods can give an approximate answer. This module approximates the solution to the Cauchy problem at equally spaced abscissas using the recurrence relation:
where . Hence the abscissas
divide a given interval
in equal segments. The previous formula is known as Euler's method.
To have a better idea how this method estimates the solution, observe in the picture below that at equally spaced abscissas segments are drawn with the slope determined by the values of for
. The curve in blue is the exact solution to the problem.

You may notice that the error increases with the index of the term in the recurrence relation, or as we get closer to the superior limit of the interval.
References
- Mihai Postolache - "Metode Numerice", Editura Sirius
- UBC Calculus Online Course Notes, http://www.ugrad.math.ubc.ca/coursedoc/math100/notes/mordifeqs/euler.html
Example 1
Next we give an example of how to use this function and display the absolute error from the exact solution. The program finds an approximate solution to the following Cauchy problem on the interval using a step of
:
which has the exact solution .
#include <codecogs/maths/calculus/ode/euler.h>
#include <stdio.h>
#include <math.h>
// precision constant
#define H 0.1
// initial value of the problem
#define Y0 1.7320508076
// limits of the approximation interval
#define A 1.0
#define B 2.0
// the given function
double f(double x, double y)
{
return y - 2*x/y;
}
// the exact solution
double exact(double x)
{
return sqrt(2*x + 1);
}
int main()
{
// compute the approximate solution
std::vector<double> sol = Maths::Calculus::ODE::euler(f, Y0, A, B, H);
// display the problem data
printf("\n");
printf("f(x, y) = y - 2*x/y\n");
printf(" y0 = %.10lf\n\n", Y0);
printf(" a = %.3lf\n", A);
printf(" b = %.3lf\n", B);
printf(" h = %.3lf\n\n", H);
// display the results, including error estimation
printf("Point Approximation Actual value Error\n\n");
// display the result
for (int i = 0; i < sol.size(); i++)
printf("x = %.1lf %.11lf %.11lf %.11lf\n",
H*i + A, sol[i], exact(H*i + A), fabs(sol[i] - exact(H*i + A)));
return 0;
}Output
f(x, y) = y - 2*x/y
y0 = 1.7320508076
a = 1.000
b = 2.000
h = 0.100
Point Approximation Actual value Error
x = 1.0 1.73205080760 1.73205080757 0.00000000003
x = 1.1 1.78978583452 1.78885438200 0.00093145252
x = 1.2 1.84584468325 1.84390889146 0.00193579179
x = 1.3 1.90040737771 1.89736659610 0.00304078160
x = 1.4 1.95363534415 1.94935886896 0.00427647518
x = 1.5 2.00567632820 2.00000000000 0.00567632820
x = 1.6 2.05666848078 2.04939015319 0.00727832759
x = 1.7 2.10674389397 2.09761769634 0.00912619763
x = 1.8 2.15603179450 2.14476105895 0.01127073555
x = 1.9 2.20466155504 2.19089023002 0.01377132502
x = 2.0 2.25276565382 2.23606797750 0.01669767633