Lesson 1 of 15

Euler's Method

Euler's Method

Differential equations describe how quantities change over time. A first-order ODE has the form:

dydt=f(t,y),y(t0)=y0\frac{dy}{dt} = f(t, y), \quad y(t_0) = y_0

We cannot always solve this analytically. Euler's method is the simplest numerical approach: approximate the solution by taking small steps along the tangent line.

The Formula

Given the current state (t,y)(t, y), take a small step of size hh:

ynext=y+hf(t,y)y_{\text{next}} = y + h \cdot f(t, y)

The idea: f(t,y)f(t, y) is the slope (derivative) at the current point. Moving hh forward in time, we step h×slopeh \times \text{slope} in the y-direction.

Example

For dydt=y\frac{dy}{dt} = y (exponential growth) with y(0)=1y(0) = 1 and h=0.1h = 0.1:

ynext=1+0.1×1=1.1y_{\text{next}} = 1 + 0.1 \times 1 = 1.1

The exact answer at t=0.1t = 0.1 is e0.11.10517e^{0.1} \approx 1.10517. Euler's method gives 1.11.1 — a reasonable approximation for a single step.

Your Task

Implement euler_step(f, t, y, h) that returns the next y value using one Euler step.

Pyodide loading...
Loading...
Click "Run" to execute your code.