Debian Codegraph @

examples/advanced/exprel_function.py

raw
1 # coding=utf-8
2 """
3 Show the improved numerical accuracy when using the `exprel` function in rate equations.
4
5 Rate equations for channel opening/closing rates often include a term of the form
6 :math:`\frac{x}{\exp(x) - 1}`. This term is problematic for two reasons:
7
8 * It is not defined for :math:`x = 0` (where it should equal to :math:`1` for
9 continuity);
10 * For values :math:`x \approx 0`, there is a loss of accuracy.
11
12 For better accuracy, and to avoid issues at :math:`x = 0`, Brian provides the
13 function `exprel`, which is equivalent to :math:`\frac{\exp(x) - 1}{x}`, but
14 with better accuracy and the expected result at :math:`x = 0`. In this example,
15 we demonstrate the advantage of expressing a typical rate equation from the HH
16 model with `exprel`.
17 """
18 from brian2 import *
19
20 # Dummy group to evaluate the rate equation at various points
21 eqs = '''v : volt
22 # opening rate from the HH model
23 alpha_simple = 0.32*(mV**-1)*(-50*mV-v)/
24 (exp((-50*mV-v)/(4*mV))-1.)/ms : Hz
25 alpha_improved = 0.32*(mV**-1)*4*mV/exprel((-50*mV-v)/(4*mV))/ms : Hz'''
26 neuron = NeuronGroup(1000, eqs)
27
28 # Use voltage values around the problematic point
29 neuron.v = np.linspace(-50 - .5e-6, -50 + .5e-6, len(neuron))*mV
30
31 fig, ax = plt.subplots()
32 ax.plot((neuron.v + 50*mV)/nvolt, neuron.alpha_simple,
33 '.', label=r'$\alpha_\mathrm{simple}$')
34 ax.plot((neuron.v + 50*mV)/nvolt, neuron.alpha_improved,
35 'k', label=r'$\alpha_\mathrm{improved}$')
36 ax.legend()
37 ax.set(xlabel='$v$ relative to -50mV (nV)', ylabel=r'$\alpha$ (Hz)')
38 ax.ticklabel_format(useOffset=False)
39 plt.tight_layout()
40 plt.show()