I have The following IVP:
$$y'=-20y+20 \cos t-\sin t ,~~~ 0≤t≤2,~~~ y(0)=0;$$
With $N = 5$, $h = 0.4$, and $t_i=0.4i$, for $i = 0,1,2,3,4,5$.
I am performing the steps as following:
1st:
$y1$=$y0+hf(t0,y0)$
$y1$=$0+0.4f(0,0)$
$y1 = 8 $
2st:
$y2$=$y1+hf(t1,y1)$
$y2$=$8+0.4f(0.4,8)$
$y2 = -48.787 $
.
.
.
$y5 = 16974.7699 $
while the exact solution is : -0.416147
I can't figure out where the mistake is,
You have a Lipschitz constant $L=20$. The Euler method demands that $Lh\le 2$, or better $Lh\le 0.1$ for stable or quantitatively useful results. This is heavily violated with $h=0.4$, as then $Lh=8$, so oscillating fast growing iterates is the expected result. Try again with $h=0.04$.
See Step size in Euler's forward method, Numerical stability of Euler Forward for a differential equation. for a more general discussion and Maximum timestep for RK4 for the same problem using RK4, with some plots