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 procedure for solving ordinary differential
equations (ODEs) with a given initial value.
Consider a differential equation dy/dx = f(x, y) with initial condition y(x0)=y0
then a successive 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
Below is the implementation:
C++
#include <iostream>
using namespace std;
float func( float x, float y)
{
return (x + y + x * y);
}
void euler( float x0, float y, float h, float x)
{
float temp = -0;
while (x0 < x) {
temp = y;
y = y + h * func(x0, y);
x0 = x0 + h;
}
cout << "Approximate solution at x = "
<< x << " is " << y << endl;
}
int main()
{
float x0 = 0;
float y0 = 1;
float h = 0.025;
float x = 0.1;
euler(x0, y0, h, x);
return 0;
}
|
Java
import java.io.*;
class Euler {
float func( float x, float y)
{
return (x + y + x * y);
}
void euler( float x0, float y, float h, float x)
{
float temp = - 0 ;
while (x0 < x) {
temp = y;
y = y + h * func(x0, y);
x0 = x0 + h;
}
System.out.println( "Approximate solution at x = "
+ x + " is " + y);
}
public static void main(String args[]) throws IOException
{
Euler obj = new Euler();
float x0 = 0 ;
float y0 = 1 ;
float h = 0 .025f;
float x = 0 .1f;
obj.euler(x0, y0, h, x);
}
}
|
Python3
def func( x, y ):
return (x + y + x * y)
def euler( x0, y, h, x ):
temp = - 0
while x0 < x:
temp = y
y = y + h * func(x0, y)
x0 = x0 + h
print ( "Approximate solution at x = " , x, " is " , "%.6f" % y)
x0 = 0
y0 = 1
h = 0.025
x = 0.1
euler(x0, y0, h, x)
|
C#
using System;
class GFG {
static float func( float x, float y)
{
return (x + y + x * y);
}
static void euler( float x0, float y, float h, float x)
{
while (x0 < x) {
y = y + h * func(x0, y);
x0 = x0 + h;
}
Console.WriteLine( "Approximate solution at x = "
+ x + " is " + y);
}
public static void Main()
{
float x0 = 0;
float y0 = 1;
float h = 0.025f;
float x = 0.1f;
euler(x0, y0, h, x);
}
}
|
PHP
<?php
function func( $x , $y )
{
return ( $x + $y + $x * $y );
}
function euler( $x0 , $y , $h , $x )
{
$temp = -0;
while ( $x0 < $x )
{
$temp = $y ;
$y = $y + $h * func( $x0 , $y );
$x0 = $x0 + $h ;
}
echo "Approximate solution at x = " ,
$x , " is " , $y , "\n" ;
}
$x0 = 0;
$y0 = 1;
$h = 0.025;
$x = 0.1;
euler( $x0 , $y0 , $h , $x );
?>
|
Javascript
<script>
function func(x, y)
{
return (x + y + x * y);
}
function euler(x0, y, h, x)
{
let temp = -0;
while (x0 < x) {
temp = y;
y = y + h * func(x0, y);
x0 = x0 + h;
}
document.write( "Approximate solution at x = "
+ x + " is " + y);
}
let x0 = 0;
let y0 = 1;
let h = 0.025;
let x = 0.1;
euler(x0, y0, h, x);
</script>
|
Output
Approximate solution at x = 0.1 is 1.11167
Time complexity: O(x/h)
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!