Exercise: Euler and RK4 by hand on a first-order ODE

Solve for y'=f(t,y) in an ODE given in implicit form and approximate y(3) with two Euler steps and one RK4 step, comparing both results.

Setup

Consider the ODE y+(t2yt)y=0y+(t^2y-t)\,y'=0 with y(1)=2y(1)=2, and approximate y(3)y(3). The equation is not in normal form, so first solve for yy':

y=f(t,y)=yt2yty'=f(t,y)=\frac{-y}{t^2y-t}

Two Euler steps

ExampleEuler with h=1

Apply two steps of explicit Euler with h=1h=1 from t0=1t_0=1, y0=2y_0=2.

  1. First step: at (1,2)(1,2) the denominator is 1221=11^2\cdot 2-1=1, so f(1,2)=2/1=2f(1,2)=-2/1=-2.

    y1=y0+hf(1,2)=2+1(2)=0y_1=y_0+h\,f(1,2)=2+1\cdot(-2)=0
  2. Second step: at (2,0)(2,0) the numerator is 0=0-0=0, hence f(2,0)=0f(2,0)=0 and the solution does not change.

    y2=y1+hf(2,0)=0+0=0y_2=y_1+h\,f(2,0)=0+0=0

Euler with h=1h=1 gives y(3)0y(3)\approx 0. The step is too large: the first jump takes the solution to y=0y=0, a point where ff vanishes and the method stays.

One Runge-Kutta step

ExampleRK4 with h=2

Apply one step of classical Runge-Kutta with h=2h=2 from t0=1t_0=1, y0=2y_0=2.

  1. Initial slope: k1=f(1,2)=2k_1=f(1,2)=-2.

  2. Midpoint (t=2t=2) advancing with k1k_1: y=2+1(2)=0y=2+1\cdot(-2)=0, so k2=f(2,0)=0k_2=f(2,0)=0.

  3. Midpoint again, now with k2k_2: y=2+10=2y=2+1\cdot 0=2, so k3=f(2,2)=2422=13k_3=f(2,2)=\frac{-2}{4\cdot 2-2}=-\frac{1}{3}.

  4. Final endpoint (t=3t=3) with k3k_3: y=2+2(13)=43y=2+2\cdot(-\tfrac13)=\tfrac43, so k4=f(3,43)=4/39433=427k_4=f\bigl(3,\tfrac43\bigr)=\frac{-4/3}{9\cdot\frac43-3}=-\frac{4}{27}.

  5. Combination with weights 1,2,2,11,2,2,1:

    y1=2+26(2+20+2(13)427)=27681=8681y_1=2+\frac{2}{6}\Bigl(-2+2\cdot 0+2\cdot\bigl(-\tfrac13\bigr)-\tfrac{4}{27}\Bigr)=2-\frac{76}{81}=\frac{86}{81}

RK4 gives y(3)8681=1.0617y(3)\approx\frac{86}{81}=1.0617. With the same number of evaluations as four Euler steps, it avoids the collapse at y=0y=0 and produces a reasonable approximation.