-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample-3.19-fitzhugh_nagumo.py
75 lines (57 loc) · 1.78 KB
/
example-3.19-fitzhugh_nagumo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# example-3.19-fitzhugh_nagumo.py - nerve cell dynamics
# RMM, 30 Aug 2021
#
# Figure 3.28: Response of a neuron to a current input. The current
# input is shown in (a) and the neuron voltage V in (b). The
# simulation was done using the FitzHugh–Nagumo model (Exercise 3.11).
import control as ct
import numpy as np
import matplotlib.pyplot as plt
ct.use_fbs_defaults()
#
# System dynamics
#
# FitzHugh-Nagumo dynamics (from KJA)
def fitzhugh_nagumo_dynamics(t, x, u, params):
dx = np.zeros(3)
# Get the system state
V = x[0]
R = x[1]
# Compute the dim derivative
dx[0] = 10 * (V - (V**3) / 3 - R + u[0])
dx[1] = 0.8 * (-R + 1.25 * V + 1.5)
dx[2] = 1
return dx
# Set up an input/output system
sys = ct.nlsys(
updfcn=fitzhugh_nagumo_dynamics, states=3, inputs=1, outputs=3)
# Set up the plotting grid to match the layout in the book
fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(3, 2)
#
# (a) Current input
#
fig.add_subplot(gs[0, 0]) # first row, first column
# Set up input stimulation
t = np.linspace(0, 50, 500)
u = np.zeros_like(t)
u[t >= 5] = 1.5 # start of short input pulse
u[t >= 6] = 0 # end of short input pulse
u[t >= 30] = 1.5 # longer input pulse
# Initial state
x0 = [-1.5, -3/8, 0]
response = ct.input_output_response(sys, t, u, x0)
plt.plot(response.time, response.inputs[0])
plt.xlabel("Time $t$ [ms]")
plt.ylabel("Current $I$ [mA]")
plt.title("Input stimulation")
#
# (b) Neuron response
#
fig.add_subplot(gs[0, 1]) # first row, second column
plt.plot(response.time, response.states[0])
plt.xlabel("Time $t$ [ms]")
plt.ylabel("Voltage $V$ [mV]")
plt.title("Neuron response")
# Save the figure
plt.savefig("figure-3.19-fitzhugh_nagumo.png", bbox_inches='tight')