Método de Euler para resolver ecuaciones diferenciales

Dada una ecuación diferencial dy/dx = f(x, y) con condición inicial y(x0) = y0. Encuentre su solución aproximada usando el método de Euler .
Método de Euler: 
en matemáticas y ciencias computacionales, el método de Euler (también llamado 
método de Euler directo) es un procedimiento numérico de primer orden para resolver ecuaciones diferenciales 
ordinarias (EDO) con un valor inicial dado. 
Considere una ecuación diferencial dy/dx = f(x, y) con la condición inicial y(x0)=y0 
, entonces la aproximación sucesiva de esta ecuación puede estar dada por: 
 

y(n+1) = y(n) + h * f(x(n), y(n)) 
donde h = (x(n) – x(0)) / n 
h indica el tamaño del paso. Elegir 
valores más pequeños de h conduce a resultados más precisos 
y más tiempo de cálculo. 
 

Ejemplo : 
 

    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++

/* 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;
}

Java

// 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.

Python3

# 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)

C#

// 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.

PHP

<?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
?>

Javascript

<script>
 
// JavaScript program to find approximation of an 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)
    {
        let 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
        document.write("Approximate solution at x = "
                           + x + " is " + y);
    }
 
// Driver Code
 
    // Initial Values
    let x0 = 0;
    let y0 = 1;
    let h = 0.025;
   
    // Value of x at which we need approximation
    let x = 0.1;
   
    euler(x0, y0, h, x);
 
// This code is contributed by chinmoy1997pal.
</script>

Producción : 
 

Approximate solution at x = 0.1  is  1.11167

Publicación traducida automáticamente

Artículo escrito por Sharad_Bhardwaj y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *