Euler Method for solving differential equation

Given a differential equation dy/dx = f(x, y) with initial condition y(x0) = y0. Find its approximate solution using Euler method.

Euler Method :
In mathematics and computational science, the Euler method (also called forward
Euler method) is a first-order numerical procedurefor solving ordinary differential
equations (ODEs) with a given initial value.
Consider a differential equation dy/dx = f(x, y) with initialcondition y(x0)=y0
then succesive approximation of this equation can be given by:

y(n+1) = y(n) + h * f(x(n), y(n))

where h = (x(n) – x(0)) / n
h indicates step size. Choosing smaller
values of h leads to more accurate results
and more computation time.

Example :


    Consider below differential equation
            dy/dx = (x + y + xy)
    with initial condition y(0) = 1 
    and step size h = 0.025.
    Find y(0.1).
   
    Solution:
    f(x, y) = (x + y + xy)
    x0 = 0, y0 = 1, h = 0.025
    Now we can calculate y1 using Euler formula
    y1 = y0 + h * f(x0, y0)
    y1 = 1 + 0.025 *(0 + 1 + 0 * 1)
    y1 = 1.025
    y(0.025) = 1.025.
    Similarly we can calculate y(0.050), y(0.075), ....y(0.1).
    y(0.1) = 1.11167

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

/* CPP  Program to find approximation
   of a ordinary differential equation
   using euler method.*/
#include <iostream>
using namespace std;
  
// Consider a differential equation
// dy/dx=(x + y + xy)
float func(float x, float y)
{
    return (x + y + x * y);
}
  
// Function for Euler formula
void euler(float x0, float y, float h, float x)
{
    float temp = -0;
  
    // Iterating till the point at which we
    // need approximation
    while (x0 < x) {
        temp = y;
        y = y + h * func(x0, y);
        x0 = x0 + h;
    }
  
    // Printing approximation
    cout << "Approximate solution at x = "
         << x << "  is  " << y << endl;
}
  
// Driver program
int main()
{
    // Initial Values
    float x0 = 0;
    float y0 = 1;
    float h = 0.025;
  
    // Value of x at which we need approximation
    float x = 0.1;
  
    euler(x0, y0, h, x);
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Code to find approximation
# of a ordinary differential equation
# using euler method.
  
# Consider a differential equation
# dy / dx =(x + y + xy)
def func( x, y ):
    return (x + y + x * y)
      
# Function for euler formula
def euler( x0, y, h, x ):
    temp = -0
  
    # Iterating till the point at which we
    # need approximation
    while x0 < x:
        temp = y
        y = y + h * func(x0, y)
        x0 = x0 + h
  
    # Printing approximation
    print("Approximate solution at x = ", x, " is ", "%.6f"% y)
      
# Driver Code
# Initial Values
x0 = 0
y0 = 1
h = 0.025
  
# Value of x at which we need approximation
x = 0.1
  
euler(x0, y0, h, x)

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find approximation of an ordinary
// differential equation using euler method
import java.io.*;
  
class Euler {
    // Consider a differential equation
    // dy/dx=(x + y + xy)
    float func(float x, float y)
    {
        return (x + y + x * y);
    }
  
    // Function for Euler formula
    void euler(float x0, float y, float h, float x)
    {
        float temp = -0;
  
        // Iterating till the point at which we
        // need approximation
        while (x0 < x) {
            temp = y;
            y = y + h * func(x0, y);
            x0 = x0 + h;
        }
  
        // Printing approximation
        System.out.println("Approximate solution at x = "
                           + x + " is " + y);
    }
  
    // Driver program
    public static void main(String args[]) throws IOException
    {
        Euler obj = new Euler();
        // Initial Values
        float x0 = 0;
        float y0 = 1;
        float h = 0.025f;
  
        // Value of x at which we need approximation
        float x = 0.1f;
  
        obj.euler(x0, y0, h, x);
    }
}
  
// This code is contributed by Anshika Goyal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find approximation of an ordinary
// differential equation using euler method
using System;
  
class GFG {
  
    // Consider a differential equation
    // dy/dx=(x + y + xy)
    static float func(float x, float y)
    {
        return (x + y + x * y);
    }
  
    // Function for Euler formula
    static void euler(float x0, float y, float h, float x)
    {
  
        // Iterating till the point at which we
        // need approximation
        while (x0 < x) {
            y = y + h * func(x0, y);
            x0 = x0 + h;
        }
  
        // Printing approximation
        Console.WriteLine("Approximate solution at x = "
                          + x + " is " + y);
    }
  
    // Driver program
    public static void Main()
    {
  
        // Initial Values
        float x0 = 0;
        float y0 = 1;
        float h = 0.025f;
  
        // Value of x at which we need
        // approximation
        float x = 0.1f;
  
        euler(x0, y0, h, x);
    }
}
  
// This code is contributed by Vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find approximation
// of a ordinary differential equation
// using euler method
  
// Consider a differential equation
// dy/dx=(x + y + xy)
  
function func($x, $y)
{
    return ($x + $y + $x * $y);
  
// Function for Euler formula
function euler( $x0, $y, $h, $x)
{
    $temp = -0;
  
    // Iterating till the point 
    // at which we need approximation
    while($x0 < $x
    {
        $temp = $y;
        $y = $y + $h * func($x0, $y);
        $x0 = $x0 + $h;
    }
  
    // Printing approximation
    echo "Approximate solution at x = ",
        $x, " is ", $y, "\n";
}
  
// Driver Code
  
// Initial Values
$x0 = 0;
$y0 = 1;
$h = 0.025;
  
// Value of x at which 
// we need approximation
$x = 0.1;
  
euler($x0, $y0, $h, $x);
  
  
// This code contributed by aj_36
?>

chevron_right



Output :

Approximate solution at x = 0.1  is  1.11167


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.