I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
COST (GBP)
this unit 5.00
sub units 0.00
+
0
MathsCalculusOde

euler

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

Interface

C++

Euler

 
std::vector<double>eulerdouble(*f)(double, double)[function pointer]
doubley0
doublea
doubleb
doubleh )
Consider \inline \displaystyle f:I \times \mathbb{R} \rightarrow \mathbb{R} a continuous function, where \inline \displaystyle I is a real interval and let \inline \displaystyle y_0 \in \mathbb{R} be the initial value in the Cauchy problem:

We need to find the function \inline \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:

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

MISSING IMAGE!

1/EulerMethod-378.gif cannot be found in /users/1/EulerMethod-378.gif. Please contact the submission author.

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 \inline [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 \inline [1,2] using a step of \inline h = 0.1: which has the exact solution \inline 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

fthe function which describes the Cauchy problem
y0the initial value
athe inferior limit of the interval
bthe superior limit of the interval
hthe precision constant (the step)

Returns

A vector containing approximate values of the solution at equally spaced abscissas.

Authors

Lucian Bentea (September 2006)
Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.