Numerical Analysis/ODE Exercises

Euler's Method
1) Solve differential equation y' = 3t2y in [0,1] using Euler's method with n=10,y(0)=1. Solution: We need to find the solution ODE y' = 3t2y using Euler's method.

yn+1=yn+hf(tn,yn)

We divide time span with number points to find the step size h.

h=tmax-tmin/n=1-0/10=0.1

y1=y0+hf(t0,y0)

=1.000000+.1x3x02x1

=1.000000

y2=y1+hf(t1,y1)

=1.000000+.1x3x0.12x1.000000

=1.003000

y3=y2+hf(t2,y2)

=1.003000+.1x3x0.22x1.003000

=1.0150360

y4=y3+hf(t3,y3)

=1.01503600+.1x3x0.32x1.0150360

=1.0424420

y5=y4+hf(t4,y4)

=1.0424420+.1x3x0.42x1.0424420

=1.0924792

y6=y5+hf(t5,y5)

=1.0924792+.1x3x0.52x1.0924792

=1.1744151

y7=y6+hf(t6,y6)

=1.1744151+.1x3x0.62x1.1744151

=1.3012520

y8=y7+hf(t7,y7)

=1.3012520+.1x3x0.72x1.3012520

=1.4925360

y9=y8+hf(t8,y8)

=1.4925360+.1x3x0.82x1.4925360

=1.7791029

y10=y9+hf(t9,y9)

=1.77910290+.1x3x0.92x1.7791029

=2.2114249

Runge-Kutta Second Order Method (RK2)
2) Solve differential equation y' = 2ty in [0,1] using RK2 method yn+1=yn+h/4(k1+3K2) where k1=f(tn,yn),k2=f(tn+2/3h,yn+2/3hk1)with n=5,y(0)=1.

Solution: We need to find the solution ODE y' = 2ty using RK second order method.

yn+1=yn+h/4(k1+3K2)

k1=f(tn,yn)

k2=f(tn+2/3h,yn+2/3hk1)

y1=y0+h/4(k1+3K2)

k1=f(t0,y0)

=2x0.0x1

=0

k2=f(t0+2/3h,y0+2/3hk1)

=2x2/3x0.2x1

=0.26667

y1=y0+h/4(k1+3K2)

=1+0.2/4(0+3x0.26667)

=1.04

y2=y1+h/4(k1+3K2)

k1=f(t1,y1)

=2x0.2x1.04

=0.416

k2=f(t1+2/3h,y1+2/3hk1)

=f(0.2+0.13333,1.04+0.0554667)

=0.730304

y2=y1+h/4(k1+3K2)

=1.04+.2/4(0.416+3x0.730304)

=1.17035

y3=y2+h/4(k1+3K2)

k1=f(t2,y2)

=2x0.4x1.17035

=0.93648

k2=f(t2+2/3h,y2+2/3hk1)

=f(0.4+2/3(0.2),1.17035+2/3(0.2)(0.93648))

=1.34316

y3=y2+h/4(k1+3K2)

=1.17035+0.2/4(0.93648+3(1.34318))

=1.41865

y4=y3+h/4(k1+3K2)

k1=f(t3,y3)

=f(0.6,1.41865)

= 1.70238

k2=f(t3+2/3h,y3+2/3xhxk1)

=f(.6+.133333,1.41865+2/3(0.2)(1.70238))

=1.64563

y4=y3+h/4(k1+3K2)

=1.41865+.2/4(1.72038+3(1.64563))

=1.75061

y5=y4+h/4(k1+3Kundefined)

k1=f(t4,y4)

=f(0.8,1.75061)

= 2.800098

k2=f(t4+2/3h,y4+2/3hk1)

=f(0.8+.133333,1.75061+2/3(0.2)(2.800098))

=3.96472

y5=y4+h/4(k1+3K2)

=1.75061+.2/4(2.800098+3(3.96472))

=2.48532

y6=y5+h/4(k1+3K2)

k1=f(t5,y5)

=f(1,2.48532)

= 4.97064

k2=f(t5+2/3h,y5+2/3hk1)

=f(1+.133333,2.48532+2/3(0.2)(4.97064))

=7.1356

y6=y5+h/4(k1+3K2)

=2.48532+.2/4(4.97064+3(7.1356))

=3.80420

Adams-Bashforth Two Step
3) Solve differential equation y' = 2ty in [0,1] using Two step Adams Bashforth method with n=5,y(0)=1.

Solution: We need to find the solution ODE y' = 2ty using Two step Adams Bashforth method.

yn+2 = yn+1 + 3/2 x hf(tn+1,yn+1)-1/2 x hf(tn,yn)

Calculating h=1-0/5=0.2

We need two approxmations for calculating y2

Calculating y1 using RK second order method.

y1=1.04

y2 = y1 + 3/2 x hf(t1,y1)-1/2 x hf(t0,y0)

=1.04+3/2(.2)f(0.2,1.04)-1/2(0.2)f(0.0,1)

=1.1648

y3 = y2 + 3/2 x hf(t2,y2)-1/2 x hf(t1,y1)

=1.1648+3/2(.2)f(0.4,1.1648)-1/2(0.2)f(0.2,1.04)

=1.40275

y4 = y3 + 3/2 x hf(t2,y2)-1/2 x hf(t2,y2)

=1.40275+3/2(.2)f(0.6,1.40275)-1/2(0.2)f(0.4,1.1648)

=1.81456

y5 = y4 + 3/2 x hf(t4,y4)-1/2 x hf(t3,y3)

=1.81456+3/2(.2)f(0.8,1.81456)-1/2(0.2)f(0.6,1.40275)

=2.21333

y6 = y5 + 3/2 x hf(t5,y5)-1/2 x hf(t4,y4)

=2.21333+3/2(.2)f(1,2.21333)-1/2(0.2)f(0.8,1.81456)

=3.25098

Runge-Kutta Fourth Order (RK4)
4) Solve the ODE $$y'=2ty$$ on the interval $$[0,1]$$, using three steps of the Runge-Kutta 4 method with $$n=2$$ and $$y(0)=1$$.

Solution: The RK4 method is given by

where
 * $$k_1 = hf(t_, y_n),$$
 * $$k_2 = hf(t_n + h/2, y_n + k_1/2),$$
 * $$k_3 = hf(t_n + h/2, y_n + k_2/2),$$ and
 * $$k_4 = hf(t_n + h/2, y_n + k_3).$$

The step size $$h$$, is found using the interval $$[0,1]$$ and $$n=2,$$ and is given by

$$ h = \frac{(1 - 0)}{n} = \frac{(1 - 0)}{2} = 0.5. $$

And we know that $$y_0 = y(0) = 1,$$ so now we can now use the RK4 formula from Eqn.($$) to find each following iteration.

$$y_1:$$

We must first solve for k1, k2, k3, and k4.

k1 = (.5)(2)(0)(1) = 0;

k2 = (.5)(2)(.25)(1) = .25;

k3 = (.5)(.25)(1.125) = .140625;

k4 = (.5)(.5)(1.140625) = .28515625.

Applying these k, and y0 to Eqn.($$), we find

y1 = 1 + 1/6(0 + (2)(.25) + (2)(.140625) + .28515625) = 1.177734375.

$$y_2:$$

Similarly, we find k1, k2, k3, and k4.

k1 = (.5)(.5)(1.177734375) = .294433593;

k2 = (.5)(.75)(1.324951172) = .496856689

k3 = (.5)(.75)(1.42616272) = .534811019;

k4 = (.5)(1)(1.712545394) = .856272697.

Applying these k, and y1 to Eqn.($$), we find

y2 = 1.177734375 + 1/6(.294433593 + (2)(.496856689) + (2)(.534811019) + .856272697) = 1.713407993

$$y_3:$$

Repeating in a similar fashion, we find y3.

k1 = (.5)(1)(1.713407993) = .856703996;

k2 = (.5)(1.25)(2.141759991) = 1.338599995;

k3 = (.5)(1.25)(2.38270799) = 1.489192494;

k4 = (.5)(1.5)(3.202600487) = 2.401950365.

Applying these k, and y2 to Eqn.($$), we find

y3 = 1.713407993 + 1/6(.856703996 + (2)(1.338599995) = (2)(1.489192494) + 2.401950365) = 3.19911455,

with $$y_3 = 3.19911455$$ as our final answer.