The forward (explicit) Euler method

The forward (explicit) Euler method is a first-order numerical procedure for solving ODEs with a given initial value.

The forward Euler method is said to be the simplest and most obvious numerical ODEs integrator. In fact, the simulation using the forward Euler only depends on past values of state variables and state derivatives (thus, an explicit integration algorithm).

Let:

  • t_n be the time at the nth time-step,
  • y_n be the computed solution at the nth time-step,  y_n \equiv y(t=t_n)
  • h be the step size, h = t_n - t_{n-1}, h being constant here.

Then, the forward Euler method used for solving ODEs is based on the formula:

y_{n+1} = y_n + hf(y_n,t_n)

The forward Euler method is based on a truncated Taylor series expansion. Expanding y in the neighbourhood of t = t_n, one gets:

y(t_n + h) \equiv y_{n+1}

= y(t_n) + h \frac{dy}{dt}|_{t_n} + {\mbox{O}}(h^2)

= y_n + h f(y_n,t_n) + {\mbox{O}}(h^2)

One can see above that the truncation of the Taylor series induces an error at every time-step, that is the local truncation error. The local truncation error of the forward Euler method is O(h^2). That is to say the forward Euler method is a first-order technique.

We end up this post with 2 example codes in R, and C++ both implementing the forward Euler method.

# Édouard Tallent @ TaGoMa.Tech
# December 2012
# QuantCorner: http://quantcorner.wordpress.com
# The following code implements the Forward Euler Method

# Initial value problem:
# dx/dy = x / y, with y(0) = 1.
# Let x(0) = 0 and the step size h = 0.1.
# Integrate at y(0.3) using the forward Euler method

# Forward Euler method
fwd.e <- function(f, x0, y0, xfinal, h){

    n <- (xfinal - x0) * h
    x_n <- x0
    y_n <- y0

    while (x_n < xfinal)
    {
        x_n <- x0 + h
        y_n <- y0 + h * f(x0, y0)

        x0 <- x_n;
        y0 <- y_n;

    }

    return(cat("Forward Euler: ", y_n))
}

# Call the fwd.e function
f <- function(x, y) x / y
fwd.e(f, 0, 1, 0.3, 0.1)
// Édouard Tallent @ TaGoMa.Tech
// December 2012
// QuantCorner: http://quantcorner.wordpress.com
// The following code implements the Forward Euler Method

// Initial value problem:
// dx/dy = x / y, with y(0) = 1.
// Let x(0) = 0 and the step size h = 0.1.
// Integrate at y(0.3) using the forward Euler method 

#include<iostream>
#include<functional>
#include<cmath>

// Forward Euler method
double ForwardEuler (std::function<double (double x, double y)>f, double x0, double y0, double xfinal, double h)
{
    double n = (xfinal - x0) * h;
    double x_n;
    double y_n;

    do
    {
        x_n = x0 + h;
        y_n = y0 + h * f(x0, y0);

        x0 = x_n;
        y0 = y_n;
    }
    while (x_n <= xfinal);

    return y_n;

}

// The function to integrate
double f (double x, double y)
{
    return x / y;
}

// Main function
int main()
{
    double x0 = 0.0;
    double y0 = 1.0;
    double xfinal = 0.3;
    double h = 0.1;
    double res;

    res = ForwardEuler(f, x0, y0, xfinal, h);
    std::cout << "Forward Euler:\t" << res << std::endl;

 return 0;
}
About these ads

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s