+10 XP
Time Steps and Stability
How small should dt be?
Too large (dt = 1): you overshoot and miss dynamics
Too small (dt = 0.00001): you're wasting computation for no extra accuracy
Rule of thumb: dt << tau (the neuron's time constant). For a neuron with tau_m = 10 ms, use dt β€ 1 ms.
python
import numpy as np
import matplotlib.pyplot as plt
# Simple exponential decay: dV/dt = -V / tau
V_rest = 0
tau = 10 # ms
V_init = 1
# Test different time steps
for dt in [0.1, 1, 5]: # too big, good, too big
t_max = 50
t = np.arange(0, t_max, dt)
V = np.zeros_like(t)
V[0] = V_init
for i in range(len(t)-1):
dV_dt = -V[i] / tau
V[i+1] = V[i] + dV_dt * dt
# Plot
plt.plot(t, V, 'o-', label=f'dt={dt}', markersize=3)
# Analytical solution (true answer)
t_exact = np.linspace(0, 50, 1000)
V_exact = V_init * np.exp(-t_exact / tau)
plt.plot(t_exact, V_exact, 'k--', linewidth=2, label='Analytical')
plt.legend()
plt.xlabel('Time (ms)')
plt.ylabel('Voltage (mV)')
plt.title('Euler Method: Effect of Time Step Size')
plt.show()Smaller dt follows the true curve more closely. But dt=1 ms is usually good enough for neuron models.