In this section we’ll take a brief look at a fairly simple method for approximating solutions to differential equations. We derive the formulas used by Euler’s Method and give a brief discussion of the errors in the approximations of the solutions.
Let’s recall the very basic aspect of Differential Equation.
We know,
It can be expressed as,
Combining both the equations, we get,
Summarizing the equation as,
Thus, we arrive at, .
where is the step size (the smaller, the better),
is right side of the differential equation.
Example 1: with the initial condition as
.
Here, we have two approaches to find the solution of the Differential Equation, which are as follows:
1) Analytical Solution: This is the exact solution of an ordinary differential equation.
Integrating both sides,
We get,
Therefore,
2) Numerical Solution: This is just an approximation of the solution.
We require this formula,
along with initial conditions, and
Let’s continue with Example 1, to find the Numerical Solution.
Example 1: with the initial condition as
and step-size
.
We know the formula, .
In this equation we put the values of at interval of
because of Step-Size (
) and obtain the value of
By initial conditions, we know
and
.


Here, we are assuming the slope to be same between two consecutive points of and
, whereas in the exact case the slope is changing constantly.
Example 2: Find approximate value of ‘‘ at
in five steps by taking h=0.2 for the DE
where
.
SOLUTION: and
Hence, and
,
We know the formula,
Therefore, equation can be modified as
Calculations required is put into a tabular form as shown below in Table 2.
The approximate value of at
, i.e
.

Code:
// Euler's Method
// Solving dy/dx = x + y by Euler's method
%use s2
// Defining function f(x,y) for this case
val f: BivariateRealFunction = object : AbstractBivariateRealFunction() {
override fun evaluate(x: Double, y: Double): Double {
return x+y
}
}
val x_at_0 = 0.0
val y_at_0 = 1.0
val h = 0.2
// Evaluation of h*f(x(n),y(n))
val e_term: BivariateRealFunction = object : AbstractBivariateRealFunction() {
override fun evaluate(h: Double, f_at_n: Double): Double {
return h*f_at_n
}
}
// y(n+1) = y(n) + h*f(x(n),y(n)) ..........Euler's Method
val Y_at_n_plus_1: BivariateRealFunction = object : AbstractBivariateRealFunction() {
override fun evaluate(y_at_n: Double, e_term: Double): Double {
return y_at_n + e_term
}
}
val h_term: BivariateRealFunction = object : AbstractBivariateRealFunction() {
override fun evaluate(h: Double, t: Double): Double {
return h*t
}
}
val xn: BivariateRealFunction = object : AbstractBivariateRealFunction() {
override fun evaluate(x_at_0: Double, h_term: Double): Double {
return x_at_0 + h_term
}
}
println("Solution of Differential Equation by Euler's Method")
val steps = mutableListOf(0.0, 1.0, 2.0, 3.0, 4.0) //no. of steps = 5
var y_n = y_at_0
for (index in steps){
val h_n = h_term.evaluate(h,index)
val x_n = xn.evaluate(x_at_0,h_n)
val f_n = f.evaluate(x_n,y_n)
val e_n = e_term.evaluate(h,f_n)
val y = Y_at_n_plus_1.evaluate(y_n,e_n)
y_n = y
println("Value of y at n=%f: %f".format(index,y))
}
println("The approximate value of y at x=1 is %f".format(y_n))
Output:
