Runge-Kutta 4th Order Method for ODEs | Numerical Methods Lab
Numerical Methods - Runge-Kutta Method for ODEs

Lab 6: Runge-Kutta 4th Order Method for Ordinary Differential Equations

Lab 6: Solution of ODEs using Runge-Kutta 4th Order Method

Experiment Information

Experiment: Solution of Ordinary Differential Equations using Runge-Kutta fourth order method

Course Code: Numerical Methods

Description: Complete lab report covering theory, algorithm, Python implementation and analysis of RK4 method for first order ODEs

Complete Lab Report PDF

1. Theory of Runge-Kutta 4th Order Method

1.1 Mathematical derivation of RK4

1.2 Comparison with Euler and RK2 methods

1.3 Error analysis and convergence

2. Algorithm

2.1 Step-by-step procedure for RK4

2.2 Weight calculations (k1-k4)

2.3 Step size considerations

3. Implementation

3.1 Python code with numpy and matplotlib

3.2 Input/output specifications

3.3 Comparison with analytical solution

4. Observations

4.1 Data recording table

4.2 Sample calculations

4.3 Error behavior at different step sizes

5. Results

5.1 Solution approximation

5.2 Error analysis

5.3 Graphical comparison with other methods

6. Discussion

6.1 Advantages and limitations

6.2 Comparison with other ODE methods

6.3 Applications in engineering problems

Python Implementation of Runge-Kutta 4th Order Method

rk4_function.py – Solving ODE for given function
import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
    """Differential equation: dy/dx = f(x,y)"""
    return x**2 - y  # Example function

def rk4(f, x0, y0, xn, n):
    """
    Runge-Kutta 4th order method
    f: function dy/dx = f(x,y)
    x0: initial x value
    y0: initial y value at x0
    xn: final x value
    n: number of steps
    Returns array of y values
    """
    h = (xn - x0) / n
    x = np.linspace(x0, xn, n+1)
    y = np.zeros(n+1)
    y[0] = y0
    
    for i in range(n):
        k1 = h * f(x[i], y[i])
        k2 = h * f(x[i] + h/2, y[i] + k1/2)
        k3 = h * f(x[i] + h/2, y[i] + k2/2)
        k4 = h * f(x[i] + h, y[i] + k3)
        
        y[i+1] = y[i] + (k1 + 2*k2 + 2*k3 + k4)/6
    
    return x, y

# Example usage
x0, y0 = 0, 1  # Initial condition
xn = 2.0       # Final x value
n = 10         # Number of steps

x, y = rk4(f, x0, y0, xn, n)

# Print results
print("Solution using RK4 method:")
for xi, yi in zip(x, y):
    print(f"x = {xi:.4f}, y = {yi:.6f}")

# Plot solution
plt.plot(x, y, 'b-', label='RK4 Solution')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Solution of ODE using RK4 Method')
plt.grid(True)
plt.legend()
plt.show()
          
rk4_dataset.py – Solving ODE for given data points
import numpy as np
import matplotlib.pyplot as plt

def rk4_dataset(f, x_data, y0):
    """
    Runge-Kutta 4th order for given x data points
    f: function dy/dx = f(x,y)
    x_data: array of x values (can be non-uniform)
    y0: initial y value at x_data[0]
    Returns array of y values
    """
    n = len(x_data) - 1
    y = np.zeros(n+1)
    y[0] = y0
    
    for i in range(n):
        h = x_data[i+1] - x_data[i]
        k1 = h * f(x_data[i], y[i])
        k2 = h * f(x_data[i] + h/2, y[i] + k1/2)
        k3 = h * f(x_data[i] + h/2, y[i] + k2/2)
        k4 = h * f(x_data[i] + h, y[i] + k3)
        
        y[i+1] = y[i] + (k1 + 2*k2 + 2*k3 + k4)/6
    
    return y

# Example usage with non-uniform x data
def f(x, y):
    return np.sin(x) - y  # Example function

# Given x data points (can be non-uniform)
x_data = np.array([0, 0.5, 1.2, 1.8, 2.5, 3.0])
y0 = 0  # Initial condition

y = rk4_dataset(f, x_data, y0)

# Print results
print("Solution using RK4 method for given data points:")
for xi, yi in zip(x_data, y):
    print(f"x = {xi:.4f}, y = {yi:.6f}")

# Plot solution
plt.plot(x_data, y, 'ro-', label='RK4 Solution')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Solution of ODE at Given Points using RK4')
plt.grid(True)
plt.legend()
plt.show()
          
×

Disclaimer

The educational materials provided on this website are intended as supplementary resources to support your learning journey. These lab reports are sample documents designed to help students understand proper formatting and content organization.

We have made every effort to ensure the accuracy of the content. However, we recommend students to perform their own experiments and prepare original reports. These samples should be used as references only.

We respect intellectual property rights. If you believe any content should be credited differently or removed, please don’t hesitate to contact us. We’re happy to make appropriate corrections or give proper attribution.

Leave a Comment

Scroll to Top