UcenlyOpen in the app

Mathematical Applications · BE5B15MAA · Topic 12 of 13

Using Nest and NestList for numerical algorithms, Euler and Runge–Kutta method

Week 12. A numerical method is a step function applied again and again, and NestList records every step.

Open the interactive lesson

Key ideas

Nest, NestList and NestWhileList

Nest[f,x,n] returns f applied n times; NestList[f,x,n] returns the n+1 values x, f[x], …, and NestWhileList[f,x,test] stops when the test fails. Fixed-point iteration is NestWhileList[Cos,1.,Abs[Cos[#]−#]>10^−5&]; Newton’s method is NestList of x−f[x]/f′[x].

Euler method as a step function

Keep a point as {t,y}, define step[{t_,y_}]:={t+Δt, y+Δt*derivative[{t,y}]} and run NestList[step,{tStart,yStart},nSteps]. Halving Δt gives a better curve; compare it with DSolve or NDSolve.

Runge–Kutta of fourth order

One RK4 step evaluates the derivative four times: at the start (k1), at the midpoint with k1 (k2), at the midpoint with k2 (k3) and at the end with k3 (k4), and moves along the weighted average. It agrees with the Taylor series up to the fourth power of the step.

In one line: NestList is the loop of numerical analysis: write the step once, then let Nest repeat it.

Formulas

Practice with worked solutions

Question 1

Take one step of the fourth order Runge–Kutta method for y′ = y, y(0) = 1 with step h = 1. What is y₁?

y1=y0+h6 (k1+2k2+2k3+k4)y_1=y_0+\frac h6\,(k_1+2k_2+2k_3+k_4)
Worked solution

Answer: 65/24

  1. k1=1, k2=1+12⋅1=32, k3=1+12⋅32=74, k4=1+74=114k_1=1,\ k_2=1+\tfrac12\cdot1=\tfrac32,\ k_3=1+\tfrac12\cdot\tfrac32=\tfrac74,\ k_4=1+\tfrac74=\tfrac{11}4
  2. y1=1+16(1+3+72+114)=1+4124=6524y_1=1+\frac16\Bigl(1+3+\frac72+\frac{11}4\Bigr)=1+\frac{41}{24}=\frac{65}{24}

The four slopes are k1 = 1, k2 = 1.5, k3 = 1.75, k4 = 2.75, and their weighted average is moved along the step.

Question 2

One step of the fourth order Runge–Kutta method for y′ = −y, y(0) = 1 with step h = 1. What is y₁?

y1=y0+h6 (k1+2k2+2k3+k4)y_1=y_0+\frac h6\,(k_1+2k_2+2k_3+k_4)
Worked solution

Answer: 3/8

  1. k1=−1, k2=−(1−12)=−12, k3=−(1−14)=−34, k4=−(1−34)=−14k_1=-1,\ k_2=-\left(1-\tfrac12\right)=-\tfrac12,\ k_3=-\left(1-\tfrac14\right)=-\tfrac34,\ k_4=-\left(1-\tfrac34\right)=-\tfrac14
  2. y1=1+16(−1−1−32−14)=1−58=38y_1=1+\tfrac16\left(-1-1-\tfrac32-\tfrac14\right)=1-\tfrac58=\tfrac38

The four slopes are k₁ = −1, k₂ = −1/2, k₃ = −3/4 and k₄ = −1/4; their weighted mean is −5/8.

More Mathematical Applications practice in Ucenly

Sources

Written by Ucenly. Based on the BE5B15MAA course materials: