]>

Runge Kutta algorithm for numerical solution of ordinary differential equations

The solution of an ordinary differential equation (ODE) is a function. The function is of a form that it satisfies the relationship between itself and its derivatives as presented by the ODE.

Numerically solving the ODE consists of evaluating the derivative and computing the value of the function at a nearby point. This process is then repeated by evaluating the derivative at the new point and then evaluating the function at the next nearby point. The simplest finite difference algorithm for accomplishing this is called Euler's Method.

d y d t = f ( t , y ) ,    y ( t 0 ) = y 0   y 1 y 0 t 1 t 0 = f ( t 0 , y 0 ) MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaWaaSaaaeaacaWGKbGaamyEaaqaaiaadsgacaWG0baaaiabg2da9iaadAgadaqadaqaaiaadshacaGGSaGaamyEaaGaayjkaiaawMcaaiaacYcacaqGGaGaaeiiaiaadMhadaqadaqaaiaadshadaWgaaWcbaGaaGimaaqabaaakiaawIcacaGLPaaacqGH9aqpcaWG5bWaaSbaaSqaaiaaicdaaeqaaOGaaeiiaiabgkziUoaalaaabaGaamyEamaaBaaaleaacaaIXaaabeaakiabgkHiTiaadMhadaWgaaWcbaGaaGimaaqabaaakeaacaWG0bWaaSbaaSqaaiaaigdaaeqaaOGaeyOeI0IaamiDamaaBaaaleaacaaIWaaabeaaaaGccqGH9aqpcaWGMbWaaeWaaeaacaWG0bWaaSbaaSqaaiaaicdaaeqaaOGaaiilaiaadMhadaWgaaWcbaGaaGimaaqabaaakiaawIcacaGLPaaaaaa@5D6C@ , thus y 1 = y 0 + h f ( t 0 , y 0 ) ,   h = t 1 t 0 MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamyEamaaBaaaleaacaaIXaaabeaakiabg2da9iaadMhadaWgaaWcbaGaaGimaaqabaGccqGHRaWkcaWGObGaamOzamaabmaabaGaamiDamaaBaaaleaacaaIWaaabeaakiaacYcacaWG5bWaaSbaaSqaaiaaicdaaeqaaaGccaGLOaGaayzkaaGaaiilaiaabccacaWGObGaeyypa0JaamiDamaaBaaaleaacaaIXaaabeaakiabgkHiTiaadshadaWgaaWcbaGaaGimaaqabaaaaa@4B96@

Applied over and over, you get y n + 1 = y n + h f ( t n , y n ) ,   h = t n + 1 t n ,   n = 0 , 1 , 2 , MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamyEamaaBaaaleaacaWGUbGaey4kaSIaaGymaaqabaGccqGH9aqpcaWG5bWaaSbaaSqaaiaad6gaaeqaaOGaey4kaSIaamiAaiaadAgadaqadaqaaiaadshadaWgaaWcbaGaamOBaaqabaGccaGGSaGaamyEamaaBaaaleaacaWGUbaabeaaaOGaayjkaiaawMcaaiaacYcacaqGGaGaamiAaiabg2da9iaadshadaWgaaWcbaGaamOBaiabgUcaRiaaigdaaeqaaOGaeyOeI0IaamiDamaaBaaaleaacaWGUbaabeaakiaacYcacaqGGaGaamOBaiabg2da9iaaicdacaGGSaGaaGymaiaacYcacaaIYaGaaiilaiablAcilbaa@58DD@

While Euler's Method is appealing for its simplicity, the algorithm suffers from a low order of accuracy, it is first order. If h MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamiAaaaa@36DA@ is halved, the error in the solution is halved. This is not acceptable for real calculations.

The most common algorithm used in practice to solve ODEs is the fourth order Runge-Kutta Method because when h MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamiAaaaa@36DA@ is halved, the error is reduced by a factor of 2 4 MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaaGOmamaaCaaaleqabaGaaGinaaaaaaa@3794@ or 16. The fourth order Runge-Kutta Method uses a more complicated algorithm for computing the derivative of the unknown function. This leads to a very accurate result in most cases.

d y d t = f ( t , y ) ,    y ( t 0 ) = y 0 ,   y n + 1 = y n + h 6 ( k 1 + 2 k 2 + 2 k 3 + k 4 ) MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaWaaSaaaeaacaWGKbGaamyEaaqaaiaadsgacaWG0baaaiabg2da9iaadAgadaqadaqaaiaadshacaGGSaGaamyEaaGaayjkaiaawMcaaiaacYcacaqGGaGaaeiiaiaadMhadaqadaqaaiaadshadaWgaaWcbaGaaGimaaqabaaakiaawIcacaGLPaaacqGH9aqpcaWG5bWaaSbaaSqaaiaaicdaaeqaaOGaaiilaiaabccacqGHsgIRcaWG5bWaaSbaaSqaaiaad6gacqGHRaWkcaaIXaaabeaakiabg2da9iaadMhadaWgaaWcbaGaamOBaaqabaGccqGHRaWkdaWcaaqaaiaadIgaaeaacaaI2aaaamaabmaabaGaam4AamaaBaaaleaacaaIXaaabeaakiabgUcaRiaaikdacaWGRbWaaSbaaSqaaiaaikdaaeqaaOGaey4kaSIaaGOmaiaadUgadaWgaaWcbaGaaG4maaqabaGccqGHRaWkcaWGRbWaaSbaaSqaaiaaisdaaeqaaaGccaGLOaGaayzkaaaaaa@6342@ where

k 1 = f ( t n , y n ) ,   k 2 = f ( t n + h 2 , y n + h k 1 2 ) ,   k 3 = f ( t n + h 2 , y n + h k 2 2 ) ,   k 4 = f ( t n + 1 , y n + h k 3 ) MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaam4AamaaBaaaleaacaaIXaaabeaakiabg2da9iaadAgadaqadaqaaiaadshadaWgaaWcbaGaamOBaaqabaGccaGGSaGaamyEamaaBaaaleaacaWGUbaabeaaaOGaayjkaiaawMcaaiaacYcacaqGGaGaam4AamaaBaaaleaacaaIYaaabeaakiabg2da9iaadAgadaqadaqaaiaadshadaWgaaWcbaGaamOBaaqabaGccqGHRaWkdaWcaaqaaiaadIgaaeaacaaIYaaaaiaacYcacaWG5bWaaSbaaSqaaiaad6gaaeqaaOGaey4kaSYaaSaaaeaacaWGObGaam4AamaaBaaaleaacaaIXaaabeaaaOqaaiaaikdaaaaacaGLOaGaayzkaaGaaiilaiaabccacaWGRbWaaSbaaSqaaiaaiodaaeqaaOGaeyypa0JaamOzamaabmaabaGaamiDamaaBaaaleaacaWGUbaabeaakiabgUcaRmaalaaabaGaamiAaaqaaiaaikdaaaGaaiilaiaadMhadaWgaaWcbaGaamOBaaqabaGccqGHRaWkdaWcaaqaaiaadIgacaWGRbWaaSbaaSqaaiaaikdaaeqaaaGcbaGaaGOmaaaaaiaawIcacaGLPaaacaGGSaGaaeiiaiaadUgadaWgaaWcbaGaaGinaaqabaGccqGH9aqpcaWGMbWaaeWaaeaacaWG0bWaaSbaaSqaaiaad6gacqGHRaWkcaaIXaaabeaakiaacYcacaWG5bWaaSbaaSqaaiaad6gaaeqaaOGaey4kaSIaamiAaiaadUgadaWgaaWcbaGaaG4maaqabaaakiaawIcacaGLPaaaaaa@76BA@ and t n = t 0 + n h MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamiDamaaBaaaleaacaWGUbaabeaakiabg2da9iaadshadaWgaaWcbaGaaGimaaqabaGccqGHRaWkcaWGUbGaamiAaaaa@3DC0@ with h MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamiAaaaa@36DA@ as the selected step size.

While this Runge-Kutta algorithm would be tedious to do on a hand-calculator, it is relatively easy to program this algorithm in many programming languages, including MATLAB. Variants of this algorithm are used in the dsolve(numerical) command in Maple and the ode45 function in MATLAB. In addition to the above steps, these computing tools estimate the error and select the value of h MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamiAaaaa@36DA@ to reduce the error. They can be used to approximate to a user specified error level value, such as 10 6 MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaaGymaiaaicdadaahaaWcbeqaaiabgkHiTiaaiAdaaaaaaa@393C@