0% found this document useful (0 votes)
122 views14 pages

4D - Runge - Kutta and Adaptive Step Size

This project compares the 4th Order Runge-Kutta Method and Adaptive Step Size Method for solving second-order ordinary differential equations related to a spring-mass system. It evaluates performance, accuracy, computational efficiency, and stability, highlighting the advantages of adaptive methods in real-world applications. The report includes algorithms, Python code implementations, and discussions on the numerical methods used.

Uploaded by

harsh.mewada22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views14 pages

4D - Runge - Kutta and Adaptive Step Size

This project compares the 4th Order Runge-Kutta Method and Adaptive Step Size Method for solving second-order ordinary differential equations related to a spring-mass system. It evaluates performance, accuracy, computational efficiency, and stability, highlighting the advantages of adaptive methods in real-world applications. The report includes algorithms, Python code implementations, and discussions on the numerical methods used.

Uploaded by

harsh.mewada22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Applied Physics Sem VI Project

Comparative analysis of 4th order Runge Kutta Method and


Adaptive Step Size Method
Harsh Mewada, UID 222259, Roll No. 220
March 2025

1
1 Abstract
We use Numerical methods to solve ordinary differential equations (ODEs) that describe physical systems,
like the motion of a spring-mass system used in this project. This report presents a comparative analysis
of the 4th Order Runge-Kutta Method and an Adaptive Step Size Method for solving second-order
differential equations that represent the dynamics of a mass attached to a spring. The objective of
my project is to analyze the performance, accuracy, computational efficiency, and stability of these two
methods.
We numerically solve the spring-mass system using 4th Order Runge-Kutta Method and an Adaptive
Step Size Method. The error propagation, computational cost, and step size variations are evaluated for
both methods. The study provides insights into when adaptive step size methods are preferable over
fixed-step RK4 in real-world applications where efficiency and precision are crucial.

2
INDEX

Sr No. Title Page No.

1 Introduction 4

2 Background 4

3 Theory 4-5

4 Algorithm and Flowcharts 5-8

5 Python Code 9-10

6 Results and Discussions 11-13

7 Conclusion/Summary 13-14

8 Applications 14

9 References 14

Table 1: Table of Contents

3
2 Introduction
The spring-mass system is a fundamental model to representing oscillatory motion in areas of mechanical
vibrations, electrical circuits, and control systems. Second-order ordinary differential equation (ODE)
represent the principles of this model and help us solve the system. Solving the system requires numerical
methods for accurate solutions.
The 4th Order Runge-Kutta method is used for its stability and accuracy, but it operates with a
fixed step size. On the other hand, Adaptive Step Size Method dynamically adjust the step size based
on error estimation that we enter into the code. It helps optimize the computational effectiveness. I
hope to analyze the accuracy of RK4 and Adaptive Step Size Method in solving the spring-mass system
and assess error of both methods.

3 Background
A spring-mass system consists of a mass m attached to a spring with stiffness k. The force exerted by
the spring follows Hooke’s Law:
F = −kx (1)
where x is the displacement of the mass. Applying Newton’s Second Law, we obtain the equation of
motion:
d2 x dx
m 2 +c + kx = F (t) (2)
dt dt
where c is the damping coefficient, and F (t) is an external force.
The system exhibits different types of motion:
• Undamped Oscillations (c = 0, F (t) = 0)
• Damped Oscillations (c > 0, F (t) = 0)
• Forced Oscillations (c > 0, F (t) ̸= 0)
Since analytical solutions are complex for most cases, numerical methods like RK4 and Adaptive step
sizeare employed to approximate the solution.
Physical systems are modeled using ODEs many times, which often do not these systems lack an
analytical solution. Numerical methods such as Euler’s Method, Runge-Kutta Methods, and Adaptive
Step Size Methods are used to approximate solutions efficiently. The choice of numerical method depends
on factors such as stability, accuracy, application and efficiency. Runge-Kutta (RK) methods achieve
the accuracy of a Taylor series approach without requiring the calculation of higher derivatives. Many
variations exist but all can be cast in the generalized form of the ODE. The Runge-Kutta family of
methods improves upon Euler’s method by using multiple function evaluations within a single step,
increasing accuracy while maintaining stability. Adaptive step size methods further enhance efficiency
by dynamically adjusting step sizes to minimize error.

4 Theory
Various types of Runge-Kutta methods can be devised by employing different numbers of terms in the
increment function as specified by n. The first-order RK method with n = 1 is, in fact, Euler’s method
The RK4 method approximates the solution of a first-order ODE:
dy
= f (t, y) (3)
dt
by computing four weighted function evaluations:
k1 = hf (tn , yn )
h k1
k2 = hf (tn + , yn + )
2 2
h k2
k3 = hf (tn + , yn + )
2 2
k4 = hf (tn + h, yn + k3 )
1
yn+1 = yn + (k1 + 2k2 + 2k3 + k4 )
6

4
where h is the step size. RK4 provides high accuracy but lacks error control.
The adaptive step size method estimates the local truncation error by computing both nth-order and
(n+1)th-order Runge-Kutta approximations simultaneously:

Error = |y5 − y4 | (4)

The step size is adjusted dynamically:


 ε 1/4
hnew = h × (5)
Error
where ε is a predefined error tolerance. This reduces computational effort by adjusting step size based
on error estimates.
Numerical stability is a crucial aspect when solving ODEs. Fixed step size methods like 4th order
Runge Kutta may have instability in stiff equations, where large step sizes lead to divergence. Adaptive
step size methods improve stability by optimizing step sizes in response to rapid changes. This helps in
geting accuracy while preventing excessive computation.
Error control is achieved in adaptive methods by comparing different order approximations and
adjusting the step size accordingly. This ensures that the numerical error remains within a user-defined
tolerance, leading to efficient and reliable solutions for complex systems.

5 Algorithm and Flowcharts


5.1 4th Order Runge Kutta Method

Algorithm 1 4th-Order Runge-Kutta Method (RK4)


1: Input: Function f (t, y), Initial time t0 , Initial value y0 , Step size h, Final time tf
2: Initialize: t = t0 , y = y0
3: while t < tf do
4: Compute k1 = hf (t, y)
5: Compute k2 = hf (t + h/2, y + k1 /2)
6: Compute k3 = hf (t + h/2, y + k2 /2)
7: Compute k4 = hf (t + h, y + k3 )
8: Update y = y + 16 (k1 + 2k2 + 2k3 + k4 )
9: Update t = t + h
10: end while
11: Output: Approximate solution y at discrete time steps

5
Start

Initialize: t = t0 , y = y0

Compute k1 = hf (t, y)

Compute k2 = hf (t + h/2, y + k1 /2)

Compute k3 = hf (t + h/2, y + k2 /2)

Compute k4 = hf (t + h, y + k3 )

Update y = y + 61 (k1 + 2k2 + 2k3 + k4 )

Update t = t + h

t < tf ? Stop

6
5.2 Adaptive Step-Size Method

Algorithm 2 Adaptive Step-Size 4th-Order Runge-Kutta Method


1: Input: Function f (t, y), Initial time t0 , Initial value y0 , Initial step size h, Final time tf , Tolerance
ϵ
2: Initialize: t = t0 , y = y0
3: while t < tf do
4: Compute k1 = hf (t, y)
5: Compute k2 = hf (t + h/2, y + k1 /2)
6: Compute k3 = hf (t + h/2, y + k2 /2)
7: Compute k4 = hf (t + h, y + k3 )
8: Compute yfull = y + 61 (k1 + 2k2 + 2k3 + k4 )
9: Compute yhalf = y + 16 (k1 + 2k2 + 2k3 + k4 )/2
10: Compute error estimate: e = ||yfull − yhalf ||
11: if e < ϵ then
12: Accept step: y = yfull , t = t + h
13: end if
14: Adjust step size: h = h × (ϵ/e)1/4
15: end while
16: Output: Approximate solution y at adaptive time steps

7
Start

Initialize: t = t0 , y = y0 , h

Compute k1 = hf (t, y)

Compute k2 = hf (t + h/2, y + k1 /2)

Compute k3 = hf (t + h/2, y + k2 /2)

Compute k4 = hf (t + h, y + k3 )

Compute yfull

Compute yhalf

Compute error e

e < ϵ? Accept step: Update y, t

Adjust step size h

t < tf ? Stop

8
6 Python Code
6.1 4th Order Runge Kutta Method
import numpy as np
import matplotlib . pyplot as plt

def two_dof_system (t , y , m1 , m2 , k1 , k2 ) :
x1 , v1 , x2 , v2 = y
a1 = ( - k1 * x1 + k2 * ( x2 - x1 ) ) / m1
a2 = ( - k2 * ( x2 - x1 ) ) / m2
return np . array ([ v1 , a1 , v2 , a2 ])

def runge_kutta_4 (f , t0 , y0 , t_final , h , params ) :


t_values = np . arange ( t0 , t_final + h , h )
y_values = np . zeros (( len ( t_values ) , len ( y0 ) ) )
e rr or _es ti ma te s = np . zeros ( len ( t_values ) - 1)
y_values [0] = y0

for i in range (1 , len ( t_values ) ) :


t = t_values [ i - 1]
y = y_values [ i - 1]

k1 = h * f (t , y , * params )
k2 = h * f ( t + h /2 , y + k1 /2 , * params )
k3 = h * f ( t + h /2 , y + k2 /2 , * params )
k4 = h * f ( t + h , y + k3 , * params )

y_next = y + ( k1 + 2* k2 + 2* k3 + k4 ) / 6

y_half_step = y + ( k1 + 2* k2 + 2* k3 + k4 ) / 12

error = np . linalg . norm ( y_next - y_half_step ) / ( np . linalg . norm ( y_next ) + 1e -10)


e rror _e s ti ma t es [ i - 1] = error

y_values [ i ] = y_next

return t_values , y_values , e rr o r_ es ti m at es

m1 , m2 = 1.0 , 1.0
k1 , k2 = 10.0 , 15.0
t0 , t_final = 0 , 10
h = 0.01
y0 = [1.0 , 0.0 , -1.0 , 0.0]

t_values , y_values , er ro r_ e st im at e s = runge_kutta_4 ( two_dof_system , t0 , y0 , t_final , h ,


( m1 , m2 , k1 , k2 ) )

print ( " First ␣ 10 ␣ Steps : " )


for i in range (10) :
print ( f " Step ␣ { i +1}: ␣ Time ␣ = ␣ { t_values [ i ]:.5 f } , ␣ x1 ␣ = ␣ { y_values [i , ␣ 0]:.5 f } , ␣ x2 ␣ = ␣ {
y_values [i , ␣ 2]:.5 f } , ␣ Error ␣ = ␣ { er ro r _e st im a te s [ i ]:.5 e } " )

plt . figure ( figsize =(10 , 5) )


plt . plot ( t_values , y_values [: , 0] , label = ’ x1 ␣ ( Mass ␣ 1) ’)
plt . plot ( t_values , y_values [: , 2] , label = ’ x2 ␣ ( Mass ␣ 2) ’)
plt . xlabel ( ’ Time ␣ ( s ) ’)
plt . ylabel ( ’ Displacement ’)
plt . legend ()
plt . grid ()
plt . show ()

plt . figure ( figsize =(10 , 5) )


plt . plot ( t_values [: -1] , error_estimates , label = ’ Error ␣ Estimation ’)
plt . xlabel ( ’ Time ␣ ( s ) ’)
plt . ylabel ( ’ Error ’)
plt . yscale ( ’ log ’)
plt . legend ()
plt . grid ()
plt . show ()

Listing 1: Python Implementation of 4th order Runge Kutta Method for Two-DOF System

9
6.2 Adaptive Step size Method
import numpy as np
import matplotlib . pyplot as plt

def two_dof_system (t , y , m1 , m2 , k1 , k2 ) :
x1 , v1 , x2 , v2 = y
a1 = ( - k1 * x1 + k2 * ( x2 - x1 ) ) / m1
a2 = ( - k2 * ( x2 - x1 ) ) / m2
return np . array ([ v1 , a1 , v2 , a2 ])

def r u n g e _ k u t t a _ f e h l b e r g (f , t0 , y0 , t_final , h0 , tol , params ) :


t = t0
y = np . array ( y0 , dtype = np . float64 )
h = h0
t_values = [ t ]
y_values = [ y . copy () ]
error_values = []

while t < t_final :


if t + h > t_final :
h = t_final - t

k1 = h * f (t , y , * params )
k2 = h * f ( t + h /4 , y + k1 /4 , * params )
k3 = h * f ( t + 3* h /8 , y + 3* k1 /32 + 9* k2 /32 , * params )
k4 = h * f ( t + 12* h /13 , y + 1932* k1 /2197 - 7200* k2 /2197 + 7296* k3 /2197 , * params )
k5 = h * f ( t + h , y + 439* k1 /216 - 8* k2 + 3680* k3 /513 - 845* k4 /4104 , * params )
k6 = h * f ( t + h /2 , y - 8* k1 /27 + 2* k2 - 3544* k3 /2565 + 1859* k4 /4104 - 11* k5 /40 ,
* params )

y_rk4 = y + (25* k1 /216 + 1408* k3 /2565 + 2197* k4 /4104 - k5 /5)


y_rk5 = y + (16* k1 /135 + 6656* k3 /12825 + 28561* k4 /56430 - 9* k5 /50 + 2* k6 /55)

error = np . linalg . norm ( y_rk5 - y_rk4 ) / ( np . linalg . norm ( y_rk5 ) + 1e -10)

if error < tol :


t += h
y = y_rk5
t_values . append ( t )
y_values . append ( y . copy () )
error_values . append ( error )

h = h * min (2 , max (0.5 , 0.9 * ( tol / ( error + 1e -10) ) **0.25) )

return np . array ( t_values ) , np . array ( y_values ) , np . array ( error_values )

m1 , m2 = 1.0 , 1.0
k1 , k2 = 10.0 , 15.0
t0 , t_final = 0 , 10
h0 = 0.1
tol = 1e -5
y0 = [1.0 , 0.0 , -1.0 , 0.0]

t_values , y_values , error_values = r u n g e _ k u t t a _ f e h l b e r g ( two_dof_system , t0 , y0 , t_final ,


h0 , tol , ( m1 , m2 , k1 , k2 ) )

plt . figure ( figsize =(10 , 5) )


plt . plot ( t_values , y_values [: , 0] , label = ’ x1 ␣ ( Mass ␣ 1) ’)
plt . plot ( t_values , y_values [: , 2] , label = ’ x2 ␣ ( Mass ␣ 2) ’)
plt . xlabel ( ’ Time ␣ ( s ) ’)
plt . ylabel ( ’ Displacement ’)
plt . legend ()
plt . grid ()
plt . show ()

plt . figure ( figsize =(10 , 5) )


plt . plot ( t_values [1:] , error_values , label = ’ Error ␣ Estimation ’)
plt . xlabel ( ’ Time ␣ ( s ) ’)
plt . ylabel ( ’ Error ’)
plt . yscale ( ’ log ’)
plt . legend ()
plt . grid ()

10
plt . show ()

# Display first 10 n u m e r i c a l results without table


for i in range ( min (10 , len ( t_values ) ) ) :
print ( f " Time : ␣ { t_values [ i ]:.3 f } ␣s , ␣ x1 : ␣ { y_values [i , ␣ 0]:.3 f } , ␣ x2 : ␣ { y_values [i , ␣ 2]:.3 f
} , ␣ Error : ␣ { error_values [i -1]:.3 e } " if i > 0 else f " Time : ␣ { t_values [ i ]:.3 f } ␣s , ␣ x1
: ␣ { y_values [i , ␣ 0]:.3 f } , ␣ x2 : ␣ { y_values [i , ␣ 2]:.3 f } , ␣ Error : ␣ N / A " )

Listing 2: Python Implementation of Adaptive Step size Method for Two-DOF System

7 Results and Discussion


7.1 4th Order Runge Kutta Method

Figure 1: Numerical solution for the spring-mass system using RK4.

Figure 2: Numerical solution for the spring-mass system using RK4.

11
Time (s) x1 x2 Error
0.00000 1.00000 -1.00000 1.66840e-01
0.01000 0.99800 -0.99850 1.44540e-01
0.02000 0.99201 -0.99401 1.21172e-01
0.03000 0.98205 -0.98654 1.01463e-01
0.04000 0.96815 -0.97611 8.58420e-02
0.05000 0.95038 -0.96277 7.35462e-02
0.06000 0.92878 -0.94656 6.37466e-02
0.07000 0.90344 -0.92754 5.57928e-02
0.08000 0.87446 -0.90578 4.92129e-02
0.09000 0.84193 -0.88134 4.36700e-02

Table 2: Numerical results of the spring-mass system using the 4th Order Runge-Kutta Method.

7.2 Adaptive Step-Size Method

Figure 3: Numerical solution for the spring-mass system using Adaptive step size method.

12
Figure 4: Numerical solution for the spring-mass system using Adaptive step size method.

Time (s) x1 x2 Error


0.000 1.000 -1.000 N/A
0.050 0.950 -0.963 9.142 × 10−6
0.096 0.821 -0.865 3.565 × 10−6
0.150 0.582 -0.686 5.015 × 10−6
0.207 0.249 -0.434 4.725 × 10−6
0.269 -0.158 -0.124 4.428 × 10−6
0.338 -0.595 0.214 3.370 × 10−6
0.401 -0.925 0.476 4.947 × 10−6
0.450 -1.099 0.623 3.931 × 10−6
0.491 -1.180 0.701 3.983 × 10−6

Table 3: Numerical results of the spring-mass system using Adap+tive Step Size Method.

8 Conclusion and Summary


8.1 Summary
This project aimed to analyze and compare the performance of the 4th Order Runge-Kutta (RK4)
method and the Adaptive Runge-Kutta-Fehlberg (RK45) method for solving a spring-mass
system governed by second-order differential equations. The equations were transformed into a system
of first-order ODEs and numerically solved using both methods.
Key aspects of the study included:
• Accuracy: RK45 provided error estimation, allowing for better precision control.
• Computational Efficiency: RK4 used a fixed step size, whereas RK45 adapted the step
size, leading to different computational costs.
• Error Analysis: The error estimation in RK45 helped optimize step sizes dynamically, ensuring
accurate results while reducing unnecessary computations.
• Comparison of Solutions: Both methods provided comparable displacement solutions for x1 and
x2 , but RK45 demonstrated better error control and efficiency for stiff or complex systems.
The results were visualized using displacement vs. time graphs and error estimation plots, highlighting
the trade-offs between fixed and adaptive step size methods.

13
8.2 Conclusion
From the numerical simulations and error analysis, the following conclusions were drawn:
• RK4 is reliable but lacks error estimation, making it less efficient when step sizes need to
be adjusted dynamically. It is best suited for problems where the required time step is known
beforehand.
• RK45 provides automatic step-size control, which improves efficiency in regions requiring
smaller steps while using larger steps when possible. This makes RK45 more adaptive to changing
system dynamics.
• For highly sensitive or stiff systems, RK45 performs better by reducing unnecessary
computations while maintaining accuracy.
• For simple or computationally inexpensive problems, RK4 remains a viable choice, as
it is easier to implement and does not require adaptive step control.
Overall, RK45 was found to be more efficient in balancing accuracy and computation time
due to its adaptive step-size feature. However, RK4 remains useful for straightforward problems where
a fixed step size is sufficient.

9 Applications
The findings of this project can be applied to various fields where numerical solutions of ODEs are
critical. Some key applications include:
1. Mechanical and Structural Engineering
• Modeling vibrations in mechanical systems, such as suspension bridges, vehicle sus-
pensions, and aerospace structures.
• Analyzing damped and forced oscillations in engineering systems.
2. Physics and Applied Mathematics
• Oscillatory systems in quantum mechanics, electromagnetism, and wave mechan-
ics.
• Studying coupled harmonic oscillators in condensed matter physics.
3. Control Systems and Robotics
• Designing PID controllers that require accurate numerical integration of system dynamics.
• Simulating robotic joint movements and motion control algorithms.
4. Electrical and Electronic Systems
• Analysis of RLC circuits, where second-order differential equations govern system behavior.
• Simulating power systems and signal processing applications.
5. Aerospace and Orbital Mechanics
• Trajectory prediction and spacecraft navigation using adaptive numerical methods.
• Modeling satellite dynamics and planetary motion where step-size control improves
computational efficiency.
6. Biomedical Applications
• Simulating biological rhythms such as heartbeats and neuron firing models.
• Modeling drug diffusion and biochemical reaction kinetics in medical research.

References
[1] H. D. Young, R. A. Freedman, University Physics with Modern Physics, 15th ed., Pearson, 2019.
[2] S. C. Chapra, R. P. Canale, Numerical Methods for Engineers, 7th ed., McGraw-Hill, 2014.

14

You might also like