How to resolve the algorithm Runge-Kutta method step by step in the Python programming language
How to resolve the algorithm Runge-Kutta method step by step in the Python programming language
Table of Contents
Problem Statement
Given the example Differential equation: With initial condition: This equation has an exact solution:
Demonstrate the commonly used explicit fourth-order Runge–Kutta method to solve the above differential equation.
Starting with a given
y
n
{\displaystyle y_{n}}
and
t
n
{\displaystyle t_{n}}
calculate: then:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Runge-Kutta method step by step in the Python programming language
This Python code implements the Runge-Kutta 4 (RK4) method for solving systems of first-order ordinary differential equations (ODEs). The RK4 method is a one-step method, meaning that it uses information from only the previous step to compute the solution at the current step. It is a fourth-order method, which means that the local error is proportional to the fourth power of the step size. The RK4 method is widely used for solving ODEs because it is relatively easy to implement and it provides good accuracy.
The code begins by importing the math
module, which provides mathematical functions such as sqrt()
. The rk4()
function is then defined. This function takes as input a function f
that defines the right-hand side of the ODE, the initial values x0
and y0
, the final value x1
, and the number of steps n
.
The rk4()
function first initializes the arrays vx
and vy
to store the values of x
and y
, respectively, at each step. The step size h
is then calculated as the difference between x1
and x0
divided by the number of steps n
. The initial values x0
and y0
are then assigned to the first elements of vx
and vy
, respectively.
The rk4()
function then iterates over the remaining steps, using the RK4 method to compute the values of x
and y
at each step. The RK4 method computes the values of k1
, k2
, k3
, and k4
using the function f
, and then uses these values to compute the next value of y
. The value of x
is then updated and stored in the next element of vx
, and the value of y
is stored in the next element of vy
.
After all the steps have been computed, the rk4()
function returns the arrays vx
and vy
.
The main program then calls the rk4()
function to solve the ODE y' = x * sqrt(y)
with initial conditions y(0) = 1
. The solution is stored in the arrays vx
and vy
. The program then prints the values of x
, y
, and the error y - (4 + x * x)**2 / 16
for every 10th step.
The error is calculated as the difference between the computed value of y
and the exact solution y = 4 + x * x
. The exact solution is known because the ODE is a linear ODE with constant coefficients. The error is small, which indicates that the RK4 method is providing accurate results.
Source code in the python programming language
from math import sqrt
def rk4(f, x0, y0, x1, n):
vx = [0] * (n + 1)
vy = [0] * (n + 1)
h = (x1 - x0) / float(n)
vx[0] = x = x0
vy[0] = y = y0
for i in range(1, n + 1):
k1 = h * f(x, y)
k2 = h * f(x + 0.5 * h, y + 0.5 * k1)
k3 = h * f(x + 0.5 * h, y + 0.5 * k2)
k4 = h * f(x + h, y + k3)
vx[i] = x = x0 + i * h
vy[i] = y = y + (k1 + k2 + k2 + k3 + k3 + k4) / 6
return vx, vy
def f(x, y):
return x * sqrt(y)
vx, vy = rk4(f, 0, 1, 10, 100)
for x, y in list(zip(vx, vy))[::10]:
print("%4.1f %10.5f %+12.4e" % (x, y, y - (4 + x * x)**2 / 16))
0.0 1.00000 +0.0000e+00
1.0 1.56250 -1.4572e-07
2.0 4.00000 -9.1948e-07
3.0 10.56250 -2.9096e-06
4.0 24.99999 -6.2349e-06
5.0 52.56249 -1.0820e-05
6.0 99.99998 -1.6595e-05
7.0 175.56248 -2.3518e-05
8.0 288.99997 -3.1565e-05
9.0 451.56246 -4.0723e-05
10.0 675.99995 -5.0983e-05
You may also check:How to resolve the algorithm Knapsack problem/Continuous step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Additive primes step by step in the BASIC programming language
You may also check:How to resolve the algorithm LZW compression step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Man or boy test step by step in the Nim programming language