4D - Runge - Kutta and Adaptive Step Size
4D - Runge - Kutta and Adaptive Step Size
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
1 Introduction 4
2 Background 4
3 Theory 4-5
7 Conclusion/Summary 13-14
8 Applications 14
9 References 14
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:
5
Start
Initialize: t = t0 , y = y0
Compute k1 = hf (t, y)
Compute k4 = hf (t + h, y + k3 )
Update t = t + h
t < tf ? Stop
6
5.2 Adaptive Step-Size Method
7
Start
Initialize: t = t0 , y = y0 , h
Compute k1 = hf (t, y)
Compute k4 = hf (t + h, y + k3 )
Compute yfull
Compute yhalf
Compute error e
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 ])
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
y_values [ i ] = y_next
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]
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 ])
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 )
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]
10
plt . show ()
Listing 2: Python Implementation of Adaptive Step size Method for Two-DOF System
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.
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.
Table 3: Numerical results of the spring-mass system using Adap+tive Step Size Method.
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