Approximate solution of differential equation with Runge-Kutta 4
Description:
Description
Your task in this kata is to solve numerically an ordinary differential equation. This is an equation of the form with initial condition .
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 by small steps with intervals h
with the below-mentioned formula:
This directly follows from the fact, that
which is equivalent to the geometrical meaning of derivative (a tangent of at point ).
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 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
The task
You will be given the following inputs:
x0, y0
: the initial pointh
: step, will be >0f(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 fromx0
tox1
inclusive separated by steph
. You should calculate the value ofy
for each integerk >= 0
such thatx0 + k * h <= x1
.
Float outputs are compared to reference with tolerance 1e-9
.
Similar Kata:
Stats:
Created | Mar 30, 2021 |
Published | Mar 30, 2021 |
Warriors Trained | 463 |
Total Skips | 180 |
Total Code Submissions | 403 |
Total Times Completed | 99 |
JavaScript Completions | 18 |
C Completions | 22 |
Python Completions | 41 |
Go Completions | 7 |
Rust Completions | 24 |
Haskell Completions | 6 |
C# Completions | 10 |
Total Stars | 14 |
% of votes with a positive feedback rating | 85% of 40 |
Total "Very Satisfied" Votes | 29 |
Total "Somewhat Satisfied" Votes | 10 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 27 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |