Lesson 2 of 15
Euler Integration
Euler Integration
A single Euler step gives one point. To approximate an entire trajectory, we repeat the step times from to .
Algorithm
def euler(f, t0, y0, t_end, n):
h = (t_end - t0) / n
t, y = t0, y0
ys = [y0]
for _ in range(n):
y = y + h * f(t, y)
t += h
ys.append(y)
return ys
The step size is . Smaller (more steps) gives higher accuracy, at the cost of more computation.
Accuracy vs. Step Size
Euler's method has first-order accuracy: if you halve , the global error halves too. For the ODE with :
| n steps | y(1) approximation | exact e ≈ 2.71828 |
|---|---|---|
| 10 | 2.5937 | 2.71828 |
| 100 | 2.7048 | 2.71828 |
| 1000 | 2.7169 | 2.71828 |
Your Task
Implement euler(f, t0, y0, t_end, n) that returns a list of values (including the initial value).
Pyodide loading...
Loading...
Click "Run" to execute your code.