The Forward Euler Method
The simplest ODE integrator: step along the current slope, and understand exactly how its error and stability behave.
The recipe
For y' = f(t,y), forward Euler advances by y_new = y + dt f(t,y). You evaluate the slope once and take a straight-line step. It is first-order: the error per step is order dt^2, and over a fixed interval the total error is order dt.
Worked case
Integrate y' = -2y, y(0) = 1, exact solution exp(-2t). Watch the accuracy improve linearly as dt shrinks.
import numpy as np
def euler(f,y0,t0,tf,dt):
y=y0; t=t0
while t<tf-1e-12:
y=y+dt*f(t,y); t+=dt
return y
f=lambda t,y:-2*y; exact=np.exp(-2)
for dt in [0.2,0.1,0.05]:
print(dt, round(abs(euler(f,1,0,1,dt)-exact),4)) # error halves as dt halves
Stability limit
For y' = lambda y with lambda < 0, forward Euler is stable only when |1 + dt lambda| <= 1, i.e. dt <= 2/|lambda|. Beyond that the numerical solution oscillates and diverges even though the true solution decays. This is why stiff problems punish explicit Euler.
When to use it
Euler is a teaching tool and a quick prototype, not a production integrator - its accuracy is poor and its stability region is small. Backward (implicit) Euler swaps the slope to the new point, gaining unconditional stability for decaying problems at the cost of solving an equation each step. For accuracy on smooth problems, step up to RK4.