6 kyu

Approximate solution of differential equation with Runge-Kutta 4

18 of 99pa-m

Description:

Description

Your task in this kata is to solve numerically an ordinary differential equation. This is an equation of the form dydx=f(x,y)\dfrac{dy}{dx} = f(x, y) with initial condition y(0)=x0y(0) = x_0.

Motivation and Euler's method

The method you will have to use is Runge-Kutta method of order 4. The Runge-Kutta methods are generalization of Euler's method, which solves a differential equation numerically by extending the trajectory from point (x0,y0)(x_0, y_0) by small steps with intervals h with the below-mentioned formula:

y(x0)=y0y(x+h)=y(x)+hf(x,y(x))y(x_0) = y_0 \\ y(x+h) = y(x) + h \cdot f(x, y(x))

This directly follows from the fact, that

f(x,y(x))=limh0y(x+h)y(x)(x+h)x\displaystyle f(x, y(x)) = \lim_{h \to 0}\dfrac{y(x+h) - y(x)} {(x + h) - x}

which is equivalent to the geometrical meaning of derivative (a tangent of y(x)y(x) at point xx).

By making a step arbitrary small, one may theoretically make the computation arbitrary precise. However, in practice the step is bounded by smallest floating point number, and this yields quite imprecise results, because residue in Euler's method is accumulated over a number of steps (which is usually counted in thousands/millions).

In order to reduce the residue as much as possible, one may use a generalization methods, i.e. a family of Runge-Kutta methods. An RK method performs more computations on each step, but also approximates more terms in Taylor decomposition of function f(x,y)f(x, y) and thus produces overall smaller residue. The order of RK method corresponds to precision and to a number of calculations on each step.

Further reading: Wikipedia

The actual Runge-Kutta 4

We will be using the most common RK method of order 4. It is given by formula

y(x+h)=y(x)+k1+2k2+2k3+k46y(x+h) = y(x) + \dfrac{k_1 + 2k_2 + 2k_3 + k_4}{6}
where
k1=hf(x,y)k2=hf(x+h2,y+k12)k3=hf(x+h2,y+k22)k4=hf(x+h,y+k3) k_1 = h f(x,y) \\ k_2 = h f(x+\frac{h}{2}, y+\frac{k_1}{2}) \\ k_3 = h f(x+\frac{h}{2}, y+\frac{k_2}{2}) \\ k_4 = h f(x+h, y+k_3)

The task

You will be given the following inputs:

  • x0, y0 : the initial point
  • h: step, will be >0
  • f(x,y): the value of derivative dy/dx at point (x,y)
  • x1: the x value for the final point. (x1 > x0)

Execute numerical integration with Runge-Kutta 4 and return the following:

  • An array of y values for xs from x0 to x1 inclusive separated by step h. You should calculate the value of y for each integer k >= 0 such that x0 + k * h <= x1.

Float outputs are compared to reference with tolerance 1e-9.

Algorithms
Mathematics

Stats:

CreatedMar 30, 2021
PublishedMar 30, 2021
Warriors Trained463
Total Skips180
Total Code Submissions403
Total Times Completed99
JavaScript Completions18
C Completions22
Python Completions41
Go Completions7
Rust Completions24
Haskell Completions6
C# Completions10
Total Stars14
% of votes with a positive feedback rating85% of 40
Total "Very Satisfied" Votes29
Total "Somewhat Satisfied" Votes10
Total "Not Satisfied" Votes1
Total Rank Assessments27
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • pa-m Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • cplir-c Avatar
  • dolamroth Avatar
  • jpssj Avatar
Ad