Computes an approximate solution to the Cauchy problem using Euler's method.

View versions (1)

Interface

#include <codecogs/maths/calculus/ode/euler.h>

using namespace Maths::Calculus::Ode;

Consider \displaystyle f:I \times \mathbb{R} \rightarrow \mathbb{R} a continuous function, where \displaystyle I is a real interval and let \displaystyle y_0 \in \mathbb{R} be the initial value in the Cauchy problem:

\left\{
\begin{array}{rcl}
\frac{dy}{dx} &=& f(x,y)\\ \\
y(x_0) &=& y_0, \qquad x_0 \in I.
\end{array} \right .
(1)

We need to find the function \displaystyle y:I \rightarrow \mathbb{R} 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:

y_{k+1} = y_k + hf(x_k, y_k), \qquad k \geq 0
(2)

where \displaystyle x_{k+1} - x_k = h > 0. Hence the abscissas x_0 < x_1 < x_2 < \ldots < x_n divide a given interval [a, b] in equal segments. The previous formula is known as Euler&#039;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 f(x_k, y_k) for k \geq 0. The curve in blue is the exact solution to the problem.

1/EulerMethod-378.gif

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 [a,b] interval.

References

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 [1,2] using a step of h = 0.1:

\begin{array}{rcl}
\frac{dy}{dx} &=& y - 2 \frac{x}{y}\\
\\
y(1) &=& 1.7320508076
\end{array}
(3)

which has the exact solution y = \sqrt{2x + 1}.

#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

Parameters

f
the function which describes the Cauchy problem
y0
the initial value
a
the inferior limit of the interval
b
the superior limit of the interval
h
the precision constant (the step)

Returns

A vector containing approximate values of the solution at equally spaced abscissas.
GPL Licence — free for non commercial use. See Licence details.