# Numerical Analysis/ODE Exercises
## Solve the following differential equations using the given method
### Euler's Method

1) Solve differential equation y' = 3t$^2$y in [0,1] using Euler's method with n=10,y(0)=1.
Solution:
We need to find the solution ODE y' = 3t$^2$y using Euler's method.
y$_{n+1}$=y$_n$+hf(t$_n$,y$_n$)
We divide time span with number points to find the step size h.
h=tmax-tmin/n=1-0/10=0.1
y$_1$=y$_0$+hf(t$_0$,y$_0$)
=1.000000+.1x3x0$^2$x1
=1.000000
y$_2$=y$_1$+hf(t$_1$,y$_1$)
=1.000000+.1x3x0.1$^2$x1.000000
=1.003000
y$_3$=y$_2$+hf(t$_2$,y$_2$)
=1.003000+.1x3x0.2$^2$x1.003000
=1.0150360
y$_4$=y$_3$+hf(t$_3$,y$_3$)
=1.01503600+.1x3x0.3$^2$x1.0150360
=1.0424420
y$_5$=y$_4$+hf(t$_4$,y$_4$)
=1.0424420+.1x3x0.4$^2$x1.0424420
=1.0924792
y$_6$=y$_5$+hf(t$_5$,y$_5$)
=1.0924792+.1x3x0.5$^2$x1.0924792
=1.1744151
y$_7$=y$_6$+hf(t$_6$,y$_6$)
=1.1744151+.1x3x0.6$^2$x1.1744151
=1.3012520
y$_8$=y$_7$+hf(t$_7$,y$_7$)
=1.3012520+.1x3x0.7$^2$x1.3012520
=1.4925360
y$_9$=y$_8$+hf(t$_8$,y$_8$)
=1.4925360+.1x3x0.8$^2$x1.4925360
=1.7791029
y$_{10}$=y$_9$+hf(t$_9$,y$_9$)
=1.77910290+.1x3x0.9$^2$x1.7791029
=2.2114249

### Runge-Kutta Second Order Method (RK2)

2) Solve differential equation y' = 2ty in [0,1] using RK2 method y$_{n+1}$=y$_n$+h/4(k$_1$+3K$_2$) where k$_1$=f(t$_n$,y$_n$),k$_2$=f(t$_n$+2/3h,y$_n$+2/3hk$_1$)with n=5,y(0)=1.
Solution:
We need to find the solution ODE y' = 2ty using RK second order method.
y$_{n+1}$=y$_n$+h/4(k$_1$+3K$_2$)
k$_1$=f(t$_n$,y$_n$)
k$_2$=f(t$_n$+2/3h,y$_n$+2/3hk$_1$)
y$_1$=y$_0$+h/4(k$_1$+3K$_2$)
k$_1$=f(t$_0$,y$_0$)
=2x0.0x1
=0
k$_2$=f(t$_0$+2/3h,y$_0$+2/3hk$_1$)
=2x2/3x0.2x1
=0.26667
y$_1$=y$_0$+h/4(k$_1$+3K$_2$)
=1+0.2/4(0+3x0.26667)
=1.04
y$_2$=y$_1$+h/4(k$_1$+3K$_2$)
k$_1$=f(t$_1$,y$_1$)
=2x0.2x1.04
=0.416
k$_2$=f(t$_1$+2/3h,y$_1$+2/3hk$_1$)
=f(0.2+0.13333,1.04+0.0554667)
=0.730304
y$_2$=y$_1$+h/4(k$_1$+3K$_2$)
=1.04+.2/4(0.416+3x0.730304)
=1.17035
y$_3$=y$_2$+h/4(k$_1$+3K$_2$)
k$_1$=f(t$_2$,y$_2$)
=2x0.4x1.17035
=0.93648
k$_2$=f(t$_2$+2/3h,y$_2$+2/3hk$_1$)
=f(0.4+2/3(0.2),1.17035+2/3(0.2)(0.93648))
=1.34316
y$_3$=y$_2$+h/4(k$_1$+3K$_2$)
=1.17035+0.2/4(0.93648+3(1.34318))
=1.41865
y$_4$=y$_3$+h/4(k$_1$+3K$_2$)
k$_1$=f(t$_3$,y$_3$)
=f(0.6,1.41865)
= 1.70238
k$_2$=f(t$_3$+2/3h,y$_3$+2/3xhxk$_1$)
=f(.6+.133333,1.41865+2/3(0.2)(1.70238))
=1.64563
y$_4$=y$_3$+h/4(k$_1$+3K$_2$)
=1.41865+.2/4(1.72038+3(1.64563))
=1.75061
y$_5$=y$_4$+h/4(k$_1$+3K)
k$_1$=f(t$_4$,y$_4$)
=f(0.8,1.75061)
= 2.800098
k$_2$=f(t$_4$+2/3h,y$_4$+2/3hk$_1$)
=f(0.8+.133333,1.75061+2/3(0.2)(2.800098))
=3.96472
y$_5$=y$_4$+h/4(k$_1$+3K$_2$)
=1.75061+.2/4(2.800098+3(3.96472))
=2.48532
y$_6$=y$_5$+h/4(k$_1$+3K$_2$)
k$_1$=f(t$_5$,y$_5$)
=f(1,2.48532)
= 4.97064
k$_2$=f(t$_5$+2/3h,y$_5$+2/3hk$_1$)
=f(1+.133333,2.48532+2/3(0.2)(4.97064))
=7.1356
y$_6$=y$_5$+h/4(k$_1$+3K$_2$)
=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.
y$_{n+2}$ = y$_{n+1}$ + 3/2 x hf(t$_{n+1}$,y$_{n+1}$)-1/2 x hf(t$_n$,y$_n$)
Calculating h=1-0/5=0.2
We need two approxmations for calculating y$_2$
Calculating y$_1$ using RK second order method.
y$_1$=1.04
y$_2$ = y$_1$ + 3/2 x hf(t$_1$,y$_1$)-1/2 x hf(t$_0$,y$_0$)
=1.04+3/2(.2)f(0.2,1.04)-1/2(0.2)f(0.0,1)
=1.1648
y$_3$ = y$_2$ + 3/2 x hf(t$_2$,y$_2$)-1/2 x hf(t$_1$,y$_1$)
=1.1648+3/2(.2)f(0.4,1.1648)-1/2(0.2)f(0.2,1.04)
=1.40275
y$_4$ = y$_3$ + 3/2 x hf(t$_2$,y$_2$)-1/2 x hf(t$_2$,y$_2$)
=1.40275+3/2(.2)f(0.6,1.40275)-1/2(0.2)f(0.4,1.1648)
=1.81456
y$_5$ = y$_4$ + 3/2 x hf(t$_4$,y$_4$)-1/2 x hf(t$_3$,y$_3$)
=1.81456+3/2(.2)f(0.8,1.81456)-1/2(0.2)f(0.6,1.40275)
=2.21333
y$_6$ = y$_5$ + 3/2 x hf(t$_5$,y$_5$)-1/2 x hf(t$_4$,y$_4$)
=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
    $y_{n+1}=y_{n}+1/6(k_{1}+2k_{2}+2k_{3}+k_{4}),\,$         (1)
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.(1) to find each following iteration.
$y_{1}:$
We must first solve for k$_1$, k$_2$, k$_3$, and k$_4$.
k$_1$ = (.5)(2)(0)(1) = 0;
k$_2$ = (.5)(2)(.25)(1) = .25;
k$_3$ = (.5)(.25)(1.125) = .140625;
k$_4$ = (.5)(.5)(1.140625) = .28515625.
Applying these k, and y$_0$ to Eqn.(1), we find
y$_1$ = 1 + 1/6(0 + (2)(.25) + (2)(.140625) + .28515625) = 1.177734375.
$y_{2}:$
Similarly, we find k$_1$, k$_2$, k$_3$, and k$_4$.
k$_1$ = (.5)(.5)(1.177734375) = .294433593;
k$_2$ = (.5)(.75)(1.324951172) = .496856689
k$_3$ = (.5)(.75)(1.42616272) = .534811019;
k$_4$ = (.5)(1)(1.712545394) = .856272697.
Applying these k, and y$_1$ to Eqn.(1), we find
y$_2$ = 1.177734375 + 1/6(.294433593 + (2)(.496856689) + (2)(.534811019) + .856272697) = 1.713407993
$y_{3}:$
Repeating in a similar fashion, we find y$_3$.
k$_1$ = (.5)(1)(1.713407993) = .856703996;
k$_2$ = (.5)(1.25)(2.141759991) = 1.338599995;
k$_3$ = (.5)(1.25)(2.38270799) = 1.489192494;
k$_4$ = (.5)(1.5)(3.202600487) = 2.401950365.
Applying these k, and y$_2$ to Eqn.(1), we find
y$_3$ = 1.713407993 + 1/6(.856703996 + (2)(1.338599995) = (2)(1.489192494) + 2.401950365) = 3.19911455,
with $y_{3}=3.19911455$ as our final answer.