# Solving ODEs Using Runge Kutta RK4

by admin in Differential Equations , Math, Statistics, and Optimization , MATLAB Family on April 27, 2019This code solves a single ordinary differential equation (ODE) using Standard form of fourth order Runge Kutta (RK4). A comparison between the solutions (Exact and Numerical) on one chart will be produced, with a second chart presenting the errors of these forms.

### Code Outputs:

- Chart comparison of the exact solution (if exist) with the numerical Solutions.
- Error chart of fourth order Runge Kutta.
- Maximum Error Value of fourth order Runge Kutta.

Max. Error = 2.8058e-05

### Input Requirements:

- ODE you want to solve.
- Initial Condition.
- Exact solution (optional)

### About the Method:

**Runge–Kutta methods** are a family of implicit and explicit iterative methods, which include the well-known routine called the Euler Method, used in temporal discretization for the approximate solutions of ordinary differential equations. These methods were developed around 1900 by the German mathematicians Carl Runge and Wilhelm Kutta.

### The Runge–Kutta Method:

Let an initial value problem be specified as follows:

Here **y** is an unknown function (scalar or vector) of time **t**, which we would like to approximate; we are told that **y’**, the rate at which **y** changes, is a function of **t** and of **y** itself. At the initial time **t0** the corresponding **y** value is **y0**. The function **f** and the data **t0**, **y0** are given.

Now pick a step-size ** h > 0** and define

for ** n = 0, 1, 2, 3, …**, using

*(Note: the above equations have different but equivalent definitions in different texts).*

Here** yn+1** is the RK4 approximation of **y(tn+1)**, and the next value **yn+1** is determined by the present value (**yn**) plus the weighted average of four increments, where each increment is the product of the size of the interval, ** h**, and an estimated slope specified by function

*f*on the right-hand side of the differential equation.

**k1**is the increment based on the slope at the beginning of the interval, using**y**(Euler’s method);**k2**is the increment based on the slope at the midpoint of the interval, using**y**and**k1**;**k3**is again the increment based on the slope at the midpoint, but now using**y**and**k2**;**k4**is the increment based on the slope at the end of the interval, using**y**and**k3**.

In averaging the four increments, greater weight is given to the increments at the midpoint. If **f** is independent of **y**, so that the differential equation is equivalent to a simple integral, then RK4 is Simpson’s rule.

The RK4 method is a fourth-order method, meaning that the local truncation error is on the order of **O(h^5)**, while the total accumulated error is on the order of **O(h^4)**.

In many practical applications the function **f** is independent of **t** (so called autonomous system, or time-invariant system, especially in physics), and their increments are not computed at all and not passed to function **f** , with only the final formula for **tn+1** used.

### References:

[1] Runge, Carl David Tolmé (1895), ‘Über die numerische Auflösung von Differentialgleichungen’, *Mathematische Annalen*, Springer, **46** (2): 167–178, doi:10.1007/BF01446807.

[2] Kutta, Martin (1901), *Beitrag zur näherungweisen Integration totaler Differentialgleichungen*.

[3] Ascher, Uri M.; Petzold, Linda R. (1998), *Computer Methods for Ordinary Differential Equations and Differential-Algebraic Equations*, Philadelphia: Society for Industrial and Applied Mathematics, ISBN 978-0-89871-412-8.

Share Now!