# Solving ODEs Using Second Order Runge Kutta

by admin in Differential Equations , Math, Statistics, and Optimization , MATLAB Family on April 27, 2019This code solves a single ordinary differential equation (ODE) using Second order Runge Kutta (RK2). 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 = 0.014434

### 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.

### Runge–Kutta Methods:

The generalization of the RK4 method is given by:

where:

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

To specify a particular method, one needs to provide the integer *s* (the number of stages), and the coefficients *a _{ij}* (for 1 ≤

*j*<

*i*≤

*s*),

*b*(for

_{i}*i*= 1, 2, …,

*s*) and

*c*(for

_{i}*i*= 2, 3, …,

*s*). The matrix [

*a*] is called the

_{ij}*Runge–Kutta matrix*, while the

*b*and

_{i}*c*are known as the

_{i}*weights*and the

*nodes*. These data are usually arranged in a mnemonic device, known as a

*Butcher tableau*(after John C. Butcher):

The Runge–Kutta method is consistent if

There are also accompanying requirements if one requires the method to have a certain order *p*, meaning that the local truncation error is O(*h ^{p}*

^{+1}). These can be derived from the definition of the truncation error itself. For example, a two-stage method has order 2 if

*b*

_{1}+

*b*

_{2}= 1,

*b*

_{2}

*c*

_{2}= 1/2, and

*a*

_{21}=

*c*

_{2}.

In general, if an explicit -stage Runge–Kutta method has order **P**, then it can be proven that the number of stages must satisfy **s>=p** , and if **p>=5** , then **s>=p+1**. However, it is not known whether these bounds are *sharp* in all cases; for example, all known methods of order 8 have at least 11 stages, though it is possible that there are methods with fewer stages. (The bound above suggests that there could be a method with 9 stages; but it could also be that the bound is simply not sharp.) Indeed, it is an open problem what the precise minimum number of stages **s** is for an explicit Runge–Kutta method to have order **P** in those cases where no methods have yet been discovered that satisfy the bounds above with equality. Some values which are known are:

The provable bounds above then imply that we can not find methods of orders * P = 1,2,…,6* that require fewer stages than the methods we already know for these orders. However, it is conceivable that we might find a method of order

*that has only 8 stages, whereas the only ones known today have at least 9 stages as shown in the table.*

**P = 7**### Second-Order Methods With Two Stages:

k1 = f(x,y(i))

k2 = f(x+h,y(i)+h*k1)

phi = 0.5*k1 + 0.5*k2

y(i+1) = y(i) + h*phi

### 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!