FITZHUGH_NAGUMO
Overview
The FITZHUGH_NAGUMO
function numerically solves the FitzHugh-Nagumo system of ordinary differential equations, modeling neuron action potentials with two variables: membrane potential and recovery variable. This model is a simplification of the Hodgkin-Huxley model and is widely used in computational neuroscience to study excitability and oscillations in neurons. The ODE system is:
where is the membrane potential, is the recovery variable, and are model parameters, is the time scale, and is the external current. The function uses SciPy’s ODE solver (scipy.integrate.solve_ivp ) to integrate the system from t_start
to t_end
. Only the basic two-variable model is exposed; external stimulus or spatial extensions are not supported. The integration method can be selected from several common solvers, but only the most used options are exposed. This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=FITZHUGH_NAGUMO(v_initial, w_initial, a, b, tau, i_ext, t_start, t_end, [method])
v_initial
(float, required): Initial membrane potential.w_initial
(float, required): Initial recovery variable.a
(float, required): Parameter a.b
(float, required): Parameter b.tau
(float, required): Time scale parameter.i_ext
(float, required): External current.t_start
(float, required): Start time for integration.t_end
(float, required): End time for integration.method
(str, optional, default=‘RK45’): Integration method. One ofRK45
,RK23
,DOP853
,Radau
,BDF
,LSODA
.
The function returns a 2D array (table) with columns: t
, V
, W
, representing time, membrane potential, and recovery variable at each step. If the input is invalid or integration fails, a string error message is returned.
Examples
Example 1: Basic Case
Inputs:
v_initial | w_initial | a | b | tau | i_ext | t_start | t_end | method |
---|---|---|---|---|---|---|---|---|
0.0 | 0.0 | 0.7 | 0.8 | 12.5 | 0.5 | 0.0 | 1.0 | RK45 |
Excel formula:
=FITZHUGH_NAGUMO(0.0, 0.0, 0.7, 0.8, 12.5, 0.5, 0.0, 1.0)
Expected output:
t | V | W |
---|---|---|
0.000 | 0.000 | 0.000 |
1.000 | 0.772 | 0.081 |
Example 2: With Optional Method (RK23)
Inputs:
v_initial | w_initial | a | b | tau | i_ext | t_start | t_end | method |
---|---|---|---|---|---|---|---|---|
0.0 | 0.0 | 0.7 | 0.8 | 12.5 | 0.5 | 0.0 | 1.0 | RK23 |
Excel formula:
=FITZHUGH_NAGUMO(0.0, 0.0, 0.7, 0.8, 12.5, 0.5, 0.0, 1.0, "RK23")
Expected output:
t | V | W |
---|---|---|
0.000 | 0.000 | 0.000 |
1.000 | 0.772 | 0.081 |
Example 3: All Args Specified
Inputs:
v_initial | w_initial | a | b | tau | i_ext | t_start | t_end | method |
---|---|---|---|---|---|---|---|---|
1.0 | 0.5 | 0.7 | 0.8 | 12.5 | 0.5 | 0.0 | 2.0 | RK45 |
Excel formula:
=FITZHUGH_NAGUMO(1.0, 0.5, 0.7, 0.8, 12.5, 0.5, 0.0, 2.0, "RK45")
Expected output:
t | V | W |
---|---|---|
0.000 | 1.000 | 0.500 |
2.000 | 1.590 | 0.760 |
Example 4: Different Parameters
Inputs:
v_initial | w_initial | a | b | tau | i_ext | t_start | t_end | method |
---|---|---|---|---|---|---|---|---|
-1.0 | 0.2 | 0.5 | 0.7 | 10.0 | 0.3 | 0.0 | 1.5 | RK45 |
Excel formula:
=FITZHUGH_NAGUMO(-1.0, 0.2, 0.5, 0.7, 10.0, 0.3, 0.0, 1.5)
Expected output:
t | V | W |
---|---|---|
0.000 | -1.000 | 0.200 |
1.500 | -1.527 | 0.061 |
Python Code
from typing import List, Union
from scipy.integrate import solve_ivp
def fitzhugh_nagumo(v_initial: float, w_initial: float, a: float, b: float, tau: float, i_ext: float, t_start: float, t_end: float, method: str = 'RK45') -> Union[List[List[float]], str]:
"""
Numerically solves the FitzHugh-Nagumo system of ordinary differential equations for neuron action potentials.
Args:
v_initial: Initial membrane potential.
w_initial: Initial recovery variable.
a: Parameter a.
b: Parameter b.
tau: Time scale parameter.
i_ext: External current.
t_start: Start time of integration.
t_end: End time of integration.
method: Integration method ('RK45', 'RK23', 'DOP853', 'Radau', 'BDF', 'LSODA'). Default is 'RK45'.
Returns:
2D list with header row: t, V, W. Each row contains time and variable values, or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate input types
try:
v0 = float(v_initial)
w0 = float(w_initial)
a = float(a)
b = float(b)
tau = float(tau)
i_ext = float(i_ext)
t0 = float(t_start)
t1 = float(t_end)
except Exception:
return "Invalid input: All initial values and parameters must be numbers."
if t1 <= t0:
return "Invalid input: t_end must be greater than t_start."
if tau <= 0:
return "Invalid input: tau must be positive."
if method not in ['RK45', 'RK23', 'DOP853', 'Radau', 'BDF', 'LSODA']:
return "Invalid input: method must be one of 'RK45', 'RK23', 'DOP853', 'Radau', 'BDF', 'LSODA'."
# FitzHugh-Nagumo ODE system
def fhn_ode(t, y):
v, w = y
dv_dt = v - v**3 / 3 - w + i_ext
dw_dt = (v + a - b * w) / tau
return [dv_dt, dw_dt]
# Integrate only at t0 and t1
try:
sol = solve_ivp(
fhn_ode,
[t0, t1],
[v0, w0],
method=method,
dense_output=False,
t_eval=[t0, t1]
)
except Exception as e:
return f"Integration error: {e}"
if not sol.success:
return f"Integration failed: {sol.message}"
# Format output: header row then data rows
result = [["t", "V", "W"]]
for i in range(len(sol.t)):
t_val = float(sol.t[i])
v_val = float(sol.y[0][i])
w_val = float(sol.y[1][i])
# Disallow nan/inf
if any([
not isinstance(t_val, float) or not isinstance(v_val, float) or not isinstance(w_val, float),
t_val != t_val or v_val != v_val or w_val != w_val, # NaN check
abs(t_val) == float('inf') or abs(v_val) == float('inf') or abs(w_val) == float('inf')
]):
return "Invalid output: nan or inf detected."
result.append([t_val, v_val, w_val])
return result