I have forgotten

• https://me.yahoo.com
COST (GBP)
5.00
0.00
0

euler

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

C++

Euler

 std::vectoreuler( double (*f)(double, double)[function pointer] double y0 double a double b double h )
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:

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

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.

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.