TITLE: Solution of Ordinary Differential Equations by using Euler’s, RK2
and RK4 Method.
Theory:
Initial Value Problems (IVPs):
The goal of numerical methods is to solve first-order differential equations
of the form:
𝒅𝒚/𝒅𝒙 = 𝒇(𝒙, 𝒚)
with a given initial condition 𝒚(𝒙𝟎 ) = 𝒚𝟎 . In this practical, the specific
function is: 𝒇(𝒙, 𝒚) = 𝒔𝒊𝒏(𝟐𝒙) − 𝒚 · 𝒕𝒂𝒏(𝒙)
Numerical methods find approximate values of 𝒚 at discrete points
𝒙𝟏 , 𝒙𝟐 , … , 𝒙ₙ rather than finding a continuous analytical function.
A. Euler’s Method (First Order)
Euler’s method is derived directly from the Taylor series expansion of
𝑦(x + ℎ)about 𝑡, where terms of order ℎ2 and higher are truncated. It
assumes the derivative remains
constant over the entire interval ℎ.
In this approach, the solution is
advanced by following the tangent
line at the starting point (x𝑛 , y𝑛 )
Mathematical Formulation:
𝑑𝑦
Given the initial value problem = 𝑓(x, 𝑦), the step-by-step update is:
𝑑𝑡
yn+1 = yn + h ⋅f(xn , yn )
B. Runge-Kutta 2nd Order
The Runge-Kutta 2nd Order method improves upon Euler's approach by
using a "trial step" to capture the curvature of the solution. Instead of just
following the slope at the starting point, it predicts a midpoint, calculates
the slope there, and then uses that updated information to take the final
step. This two-stage process effectively averages the slope over the
interval, significantly
reducing the "overshoot"
error common in simpler
methods. Consequently,
RK2 provides a much more
accurate trajectory for the
curve while remaining
computationally efficient for
most basic simulations.
➢ Predictor (k₁): 𝑘1 = ℎ · 𝑓(𝑥ᵢ, 𝑦ᵢ)
➢ Corrector (k₂): 𝑘2 = ℎ · 𝑓(𝑥ᵢ + ℎ, 𝑦ᵢ + 𝑘1 )
➢ Final Step:
𝑘1 + 𝑘2
𝑦𝑖+2 = 𝑦ᵢ +
2
C. Runge-Kutta 4th Order (RK4)
The Runge-Kutta 4th Order method is an iterative numerical technique
used to approximate the solutions of first-order ordinary differential
𝑑𝑦
equations (ODEs) of the form = 𝑓(𝑡, 𝑦). 𝐼𝑡 is a "single-step" method,
𝑑𝑡
meaning it only requires information from the current point (𝑡𝑛 , 𝑦𝑛 ) to
calculate the next point (𝑡𝑛+1 , 𝑦𝑛+1 ). It uses four different slope estimates
across the interval to achieve high accuracy.
• 𝒌𝟏 = ℎ · 𝑓(𝑥ᵢ, 𝑦ᵢ) (Slope at the start)
• 𝒌𝟐 = ℎ · 𝑓(𝑥ᵢ + ℎ/2, 𝑦ᵢ + 𝒌𝟏 /2) (Slope at the midpoint)
• 𝒌𝟑 = ℎ · 𝑓(𝑥ᵢ + ℎ/2, 𝑦ᵢ + 𝒌𝟐 /2)(Improved slope at the midpoint)
• 𝒌𝟒 = ℎ · 𝑓(𝑥ᵢ + ℎ, 𝑦ᵢ + 𝒌𝟑 )(Slope at the end)
𝒌𝟏 +𝟐𝒌𝟐 +𝟐𝒌𝟑 +𝒌𝟒
• 𝒚𝒊+𝟏 = 𝒚ᵢ +
𝟔
Pseudocode:
1. Define f(x, y) such that dy/dx = sin(2x) − ytan(x).
2. Define ϕ(x) for error comparison.
3. Initialize Global Constants
4. Set initial point (x0 , y0 )Set target endpoint xn .
5. Define number of intervals n.
𝐱 𝐧 −𝐱 𝟎
6. Step Size Calculation: 𝐡 = .
𝐧
For Euler Method
1. Initialize x = x0 and y = y0
2. Repeat n times:
o slope = f(x, y)
o y = y + (slope × h)
o x = x + h
o Store/Display (x, y).
For Runge-Kutta 2nd Order (RK2)
1. Initialize x = x0 and y = y0 .
2. Repeat n times:
o 𝐤 𝟏 = 𝐡 × 𝐟(𝐱, 𝐲)
o 𝐤 𝟐 = 𝐡 × 𝐟(𝐱 + 𝐡, 𝐲 + 𝐤 𝟏 )
o 𝐲 = 𝐲 + (𝐤 𝟏 + 𝐤 𝟐 )/𝟐
o 𝐱 = 𝐱 + 𝐡
o Store/Display (x, y).
For Runge-Kutta 4th Order (RK4)
1. Initialize x = x0 and y = y0 .
2. Repeat n times:
o k1 = h × f(x, y)
o k 2 = h × f(x + h/2, y + k1 /2)
o k 3 = h × f(x + h/2, y + k 2 /2)
o k 4 = h × f(x + h, y + k 3 )
o y = y + (k1 + 2k 2 + 2k 3 + k 4 )/6
o x = x + h
o Store/Display (x, y).
Function (𝒚′ = 𝒙𝟐 + 𝒚, 𝒚(𝒙) = −𝒙𝟐 − 𝟐𝒙 + 𝟒𝒆𝒙 − 𝟐, 𝒚(𝟎) = 𝟐):
Function(𝒚′ = 𝒙𝟐 + 𝟐𝒙 − 𝒚, 𝒚(𝒙) = 𝒙𝟐 + 𝒆𝟏−𝒙 , 𝒚(𝟏) = 𝟐)
Conclusion and Discussion:
The experimental results demonstrate a clear hierarchy in accuracy, with the Runge-
Kutta 4th Order (RK4) method emerging as the most precise approximation for the given
differential equation. While Euler’s method is computationally simple, it showed
significant deviation from the exact solution because its first-order logic fails to account
for the function's curvature over large steps. The RK2 method offered a middle ground,
improving accuracy by using a midpoint trial step to "correct" its trajectory. Ultimately,
the RK4 method closely matched the analytical solution ϕ(𝑥), proving that its weighted
average of four different slopes effectively minimizes truncation error. This comparison
confirms that for trigonometric functions with high curvature, higher-order methods like
RK4 are necessary to maintain stability and reliability in numerical simulations.