12. Runge-Kutta (RK4) numerical solution for Differential Equations (2024)

In the last section, Euler's Method gave us one possible approach for solving differential equations numerically.

The problem with Euler's Method is that you have to use a small interval size to get a reasonably accurate result. That is, it's not very efficient.

The Runge-Kutta Method produces a better result in fewer steps.

Runge-Kutta Method Order 4 Formula

Applications of RK4

Mechanics

  • Springs and dampeners on cars (This spring applet uses RK4.)

Biology

  • Predator-prey models
  • Fisheries collapses
  • Drug delivery
  • Epidemic prediction

Physics

  • Climate change models
  • Ozone protection

Aviation

  • On-board computers
  • Aerodynamics

`y(x+h)` `=y(x)+` `1/6(F_1+2F_2+2F_3+F_4)`

where

`F_1=hf(x,y)`

`F_2=hf(x+h/2,y+F_1/2)`

`F_3=hf(x+h/2,y+F_2/2)`

`F_4=hf(x+h,y+F_3)`

Where does this formula come from?

Here's a brief background to the formula.

Formula derivation

We learned earlier that Taylor's Series gives us a reasonably good approximation to a function, especially if we are near enough to some known starting point, and we take enough terms.

However, one of the drawbacks with Taylor's method is that you need to differentiate your function once for each new term you want to calculate. This can be troublesome for complicated functions, and doesn't work well in computerised modelling.

Carl Runge (pronounced "roonga") and Wilhelm Kutta (pronounced "koota") aimed to provide a method of approximating a function without having to differentiate the original equation.

Their approach was to simulate as many steps of the Taylor's Series method but using evaluation of the original function only.

Runge-Kutta Method of Order 2

We begin with two function evaluations of the form:

`F_1=hf(x,y)`

`F_2=hf(x+alpha h,y+beta F_1)`

The `alpha` and `beta` are unknown quantities. The idea was to take a linear combination of the `F_1` and `F_2` terms to obtain an approximation for the `y` value at `x = x_0+h`, and to find appropriate values of `alpha` and `beta`.

By comparing the values obtains using Taylor's Series method and the above terms (I will spare you the details here), they obtained the following, which is Runge-Kutta Method of Order 2:

`y(x+h)=y(x)+1/2(F_1+F_2)`

where

`F_1=hf(x,y)`

`F_2=hf(x+h,y+F_1)`

Runge-Kutta Method of Order 3

As usual in this work, the more terms we take, the better the solution. In practice, the Order 2 solution is rarely used because it is not very accurate.

A better result is given by the Order 3 method:

`y(x+h)=` `y(x)+1/9(2F_1+3F_2+4F_3)`

where

`F_1=hf(x,y)`

`F_2=hf(x+h/2,y+F_1/2)`

`F_3=hf(x+(3h)/4,y+(3F_2)/4)`

This was obtained in a similar way to the earlier formula, by comparing Taylor's Series results.

The most commonly used Runge-Kutta formula in use is the Order 4 formula (RK4), as it gives the best trade-off between computational requirements and accuracy.

Let's look at an example to see how it works.

Example

Use Runge-Kutta Method of Order 4 to solve the following, using a step size of `h=0.1` for `0lexle1`.

`dy/dx=(5x^2-y)/e^(x+y)`

`y(0) = 1`

Step 1

Note: The following looks tedious, and it is. We'll use a computer (not calculator) to do most of the work for us. The following is here so you can see how the formula is applied.

We start with `x=0` and `y=1`. We'll find the `F` values first:

`F_1=hf(x,y)` ` = 0.1(5(0)^2-1)/e^(0+1)` ` = -0.03678794411`

For `F_2`, we need to know:

`x+h/2 = 0+0.1/2 = 0.05`, and

`y+F_1/2` ` = 1 + (-0.03678794411)/2` ` = 0.98160602794`

We substitute these into the `F_2` expression:

`F_2=hf(x+h/2,y+F_1/2)` ` = 0.1((5(0.05)^2-0.98160602794)/e^(0.05+0.98160602794))` `=-0.03454223937`

For `F_3`, we need to know:

`y+F_2/2` ` = 1 + (-0.03454223937)/2` ` = 0.98272888031`

So

`F_3=hf(x+h/2,y+F_2/2)` ` = 0.1((5(0.05)^2-0.98272888031)/e^(0.05+0.98272888031))` `=-0.03454345267`

For `F_4`, we need to know:

`y+F_3` ` = 1 -0.03454345267` ` = 0.96545654732`

So

`F_4=hf(x+h,y+F_3)` ` = 0.1((5(0.1)^2-0.96545654732)/e^(0.1+0.96545654732))` `= -0.03154393258`

Step 2

Next, we take those 4 values and substitute them into the Runge-Kutta RK4 formula:

`y(x+h)` `=y(x)+1/6(F_1+2F_2+2F_3+F_4)`

`=1+1/6( -0.03678794411` ` -\ 2xx0.03454223937` `-\ 2 xx0.03454345267` `{:-\ 0.03154393258)`

`=0.9655827899`

Using this new `y`-value, we would start again, finding the new `F_1`, `F_2`, `F_3` and `F_4`, and substitute into the Runge-Kutta formula.

We continue with this process, and construct the following table of Runge-Kutta values. (I used a spreadsheet to obtain the table. Using calculator is very tedious, and error-prone.)

`x``y``F_1 = h dy/dx``x+h/2``y+F_1/2``F_2``y+F_2/2``F_3``x+h``y+F_3``F_4`
01−0.03678794410.050.9816060279−0.03454223940.9827288803−0.03454345270.10.9654565473−0.0315439326
0.10.9655827899−0.03154430.150.9498106398−0.02787692830.9516443257−0.02788679540.20.9376959945−0.023647342
0.20.937796275−0.0236481850.250.9259721824−0.01892677610.9283328869−0.01895480880.30.9188414662−0.0138576597
0.30.9189181059−0.01385886280.350.9119886745−0.00847823960.9146789861−0.00853141670.40.9103866892−0.0029773028
0.40.9104421929−0.00297863440.450.90895287560.00266043290.91177240930.0025807040.50.91302289690.0082022376
0.50.9130598390.00820103540.550.91716035670.0137273010.91992348950.01362588670.60.92668572570.018973147
0.60.92670659860.01897229760.650.93619274740.02407941970.93874630850.02396587090.70.95067246960.0287752146
0.70.95067961420.02877487180.750.96506705010.03324486160.9673020450.03313051320.80.98381012740.0372312889
0.80.98380576590.03723152450.851.00242152820.04094087471.00427620330.04083597510.91.0246417410.0441484563
0.91.0246280460.04414926080.951.04670267640.04705938071.04815773630.046971227911.07159927390.0494916177
11.0715783953

I haven't included any values after `y` in the bottom row as we won't be using them.

Here is the graph of the solutions we found, from `x=0` to `x=1`.

12. Runge-Kutta (RK4) numerical solution for Differential Equations (1)

For interest, I extended the result up to `x=10`, and here is the resulting graph:

12. Runge-Kutta (RK4) numerical solution for Differential Equations (2)

Exercise

Solve the following using RK4 (Runge-Kutta Method of Order 4) for `0 le x le 2`. Use a step size of `h=0.2`:

`dy/dx=(x+y)sin xy`

`y(0) = 5`

Answer

Step 1

We have `x_0=0` and `y_0=5`.

`F_1=hf(x,y)` ` = 0.2((0+5)sin (0)(5))` ` = 0`

For `F_2`, we need to know:

`x+h/2 = 0+0.2/2 = 0.1`, and

`y+F_1/2 = 5+0/2=5 `

We substitute these into the `F_2` expression:

`F_2=hf(x+h/2,y+F_1/2)` ` = 0.2((0.1+5)sin (0.1)(5))` `=0.48901404937`

For `F_3`, we need to know:

`y+F_2/2` ` = 5+0.48901404937/2` `=5.24450702469`

So

`F_3=hf(x+h/2,y+F_2/2) `

`= 0.2((0.1+5.24450702469)` `{:xxsin (0.1)(5.24450702469))`

`=0.53523913352`

For `F_4`, we need to know:

`y+F_3` ` = 5+0.53523913352` ` = 5.53523913352 `

So

`F_4=hf(x+h,y+F_3)`

` = 0.2((0.2+5.53523913352)` `{:xxsin (0.2)(5.53523913352))`

`= 1.02589900571`

Step 2

Next, we take those 4 values and substitute them into the Runge-Kutta RK4 formula:

`y(x+h)=y(x)+1/6(F_1+2F_2+2F_3+F_4)`

`=5+1/6(0+ ` `2xx0.48901404937+ ` `2xx0.53523913352 ` `{:+ 1.02589900571)`

`=5.5124008953`

As before, we need to take this `y_1` value and use the new `x_1=0.2` value to find the next value, `y_2`, and so on up to `x=2`.

Following is the table of resulting values.

Here's the table of values we get.

View table

`x``y``F_1 = h dy/dx``x+h/2``y+F_1/2``F_2``y+F_2/2``F_3``x+h``y+F_3``F_4`
0500.150.48901404945.24450702470.53523913350.25.53523913351.0258990057
0.25.51240089531.01946890110.36.02213534581.2294244546.12711312231.23976135730.46.75216225250.6102193187
0.46.60707753560.6703508620.56.9422529666-0.48166555386.3662447588-0.05701426020.66.5500632755-1.0142481364
0.66.3702013853-0.87713520650.75.931633782-1.12356424585.8084192624-1.03900376910.85.3311976162-1.1055304726
0.85.3189011004-1.09805145610.94.7698753724-1.03565063584.8010757825-1.053978362614.2649227378-0.9493143311
14.2811304698-0.95951844861.13.8013712455-0.84535082883.8584550553-0.88501717651.23.3961132933-0.7389191476
1.23.421268202-0.75921771551.33.0416593442-0.63045496293.1060407205-0.68822073371.42.7330474682-0.5227646767
1.42.7680459044-0.55818564581.52.4889530815-0.44507661092.5455075989-0.50665876221.62.2613871422-0.354308929
1.62.2987183509-0.39845502181.72.09949084-0.31508045452.1411781236-0.36723945631.81.9314788946-0.2454079264
1.81.9639678892-0.28867301661.91.8196313809-0.2309806121.8484775833-0.271461225921.6925066634-0.1779964411
21.7187090337

Once again, I haven't included any values after `y` in the bottom row as they are not required.

Here is the graph of the solutions we found, from `x=0` to `x=2`.

12. Runge-Kutta (RK4) numerical solution for Differential Equations (3)

For interest, I extended the result up to `x=6`, and here is the resulting graph:

12. Runge-Kutta (RK4) numerical solution for Differential Equations (4)

We observe the graph is not very smooth. If we use a smaller `h` value, the curve will generally be smoother.

In fact, this curve is asymptotic to the `x`-axis (it smoothly gets closer to the axis as `x` gets larger). The above graph shows what happens when our intervals are too coarse. (Those wiggles after around `x>4.5` should not be there.)

Here is the solution graph again, but this time I've used `h=0.1`. It's better, but still has a "wiggle" near `x=5` that should not be there.

12. Runge-Kutta (RK4) numerical solution for Differential Equations (5)

If I took even smaller values of `h`, I'd get a smoother curve. However, that's only up to a point, because rounding errors become significant eventually. Also, computing time goes up for little added benefit.

Improvements to Runge-Kutta

As you can see from the above example, there are points in the curve where the `y` values change relatively quickly (between `0` and `1`) and other places where the curve is more nearly linear (from `1` to `2`). Also, we have strange behavior near `x=5`.

In practice, when writing a computer program to perform Runge-Kutta, we allow for variable `h` values - quite small when the curve is changing quickly, and larger `h` values when the curve is relatively smooth.

Mathematics computer algebra systems (like Mathcad, Mathematica and Maple) include routines that calculate RK4 for us, so we just need to provide the function and the interval of interest.

Caution

Always be wary of your answers! Numerical solutions are only ever approximations, and under certain conditions, you can get chaotic results.

Need help solving a different Calculus problem? Try the Problem Solver.


Disclaimer: IntMath.com does not guarantee the accuracy of results. Problem Solver provided by Mathway.

12. Runge-Kutta (RK4) numerical solution for Differential Equations (2024)

FAQs

What is the formula for RK4 method? ›

The most commonly used method is Runge-Kutta fourth order method. x(1) = 1, using the Runge-Kutta second order and fourth order with step size of h = 1. yi+1 = yi + h 2 (k1 + k2), where k1 = f(xi,ti), k2 = f(xi + h, ti + hk1).

What is the numerical differentiation Runge-Kutta method? ›

Numerical Methods

Runge–Kutta method is an effective and widely used method for solving the initial-value problems of differential equations. Runge–Kutta method can be used to construct high order accurate numerical method by functions' self without needing the high order derivatives of functions.

How to find step size in Runge-Kutta method? ›

We actually use a step-size h1=0.9h(T|y′1−y″1|)1/3.

What is the RK 4 method in Python? ›

Runge-Kutta (RK4) is most commonly used method for integrating Ordinary Differential Equations (ODEs). This method takes into account slope at the beginning, middle (twice) and the end of interval to integrate an ODE with a 4th order accuracy.

Can we directly use Runge-Kutta method for solving partial differential equation? ›

It can be used. Follow these links to have an idea about numerical approach to solve any PDE. Yes, it can be used to solve the PDEs in the sense of MOL. But in such a scenario, the system is often stiff and RK 23 is more appropriate than the ordinary RK4.

Why do we use RK4 method? ›

The most commonly used Runge Kutta method to find the solution of a differential equation is the RK4 method, i.e., the fourth-order Runge-Kutta method. The Runge-Kutta method provides the approximate value of y for a given point x. Only the first order ODEs can be solved using the Runge Kutta RK4 method.

What is a Runge-Kutta solver? ›

In numerical analysis, the Runge–Kutta methods (English: /ˈrʊŋəˈkʊtɑː/ RUUNG-ə-KUUT-tah) are a family of implicit and explicit iterative methods, which include the Euler method, used in temporal discretization for the approximate solutions of simultaneous nonlinear equations.

What is the Runge-Kutta order 3 formula? ›

The General Third Order Fuzzy Runge-Kutta Method formula is yn+1 = yn + h 6 [k1 + 4k2 + k3], where k1 = f(xn,yn), k2 = f(xn + h 2 ,yn + h 2 k1), k3 = f(xn + h, yn − hk1 + 2hk2).

What is the most accurate Runge-Kutta method? ›

One of the most widely used methods for the solution of IVPs is the fourth order Runge-Kutta (RK4) technique. The LTE of this method is order h5. The method is given below. yn+1 = yn + (k1 + 2k2 + 2k3 + k4)/6.

Which numerical method is used to solve differential equations? ›

Before discussing Euler Method, we introduce the concepts of Explicit Method and Implicit Method first. These two methods are two approaches used in numerical analysis for differential equations. Both explicit method and implicit method focus on solving the Initial Value Problem.

What is the numerical method of numerical differentiation? ›

The main methods of computing Numerical differentiation are only the Forward and the Backward methods, both very accurate but involve complex maths. There are three methods of computing Numerical Differentiation: Forward, Backward and Central difference.

What is the error in the RK4 method? ›

The RK4 method is a fourth-order method, meaning that the error per step is on the order of h5, while the total accumulated error has order h4. This differential equation has been solved by Runge-Kutta fourth order method with the help of computer program at different values of x in the interval [0,1].

Is Runge-Kutta a multistep method? ›

Another class of approximations, called Runge-Kutta methods, will also be discussed briefly. These are not linear multistep methods, but the two are sometimes used in conjunction. Euler's method and the trapezium method are both special cases of a wider class of so-called linear multistep methods.

What is the first order Runge-Kutta method? ›

Key Concept: First Order Runge-Kutta Algorithm

to progress from a point at t=t0, y*(t0), by one time step, h, follow these steps (repetitively). Notes: an initial value of the function must be given to start the algorithm. see the MATLAB programs on this page for examples that implement this algorithm.

Which method is used to solve differential equations? ›

Differential Equation

Taking an initial condition, rewrite this problem as 1/f(y)dy= g(x)dx and then integrate on both sides. Integrating factor technique is used when the differential equation is of the form dy/dx + p(x)y = q(x) where p and q are both the functions of x only.

What is Runge-Kutta method for nonlinear differential equations? ›

In numerical analysis, the Runge–Kutta methods (English: /ˈrʊŋəˈkʊtɑː/ RUUNG-ə-KUUT-tah) are a family of implicit and explicit iterative methods, which include the Euler method, used in temporal discretization for the approximate solutions of simultaneous nonlinear equations.

What is the Runge-Kutta method for third order differential equations? ›

The General Third Order Fuzzy Runge-Kutta Method formula is yn+1 = yn + h 6 [k1 + 4k2 + k3], where k1 = f(xn,yn), k2 = f(xn + h 2 ,yn + h 2 k1), k3 = f(xn + h, yn − hk1 + 2hk2).

What is the equation for the Runge-Kutta method of motion? ›

In general, a Runge-Kutta method adjusts the partition of dt as well as the constants ai and ci so as to minimize the error. xn+1=xn+16(k1+2k2+2k3+k4). Because Runge-Kutta algorithms are self-starting, they are frequently used to obtain the first few iterations for an algorithm that is not self-starting.

References

Top Articles
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 5963

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.