Tutorial 1: Linear dynamical systems¶
Week 2, Day 3: Linear Systems
By Neuromatch Academy
Content Creators: Bing Wen Brunton, Alice Schwarze
Content Reviewers: Norma Kuhn, Karolina Stosio, John Butler, Matthew Krause, Ella Batty, Richard Gao, Michael Waskom
Production editors: Gagana B, Spiros Chavlis
Tutorial Objectives¶
Estimated timing of tutorial: 1 hour
In this tutorial, we will be learning about behavior of dynamical systems -- systems that evolve in time -- where the rules by which they evolve in time are described precisely by a differential equation.
Differential equations are equations that express the rate of change of the state variable . One typically describes this rate of change using the derivative of with respect to time () on the left hand side of the differential equation:
A common notational short-hand is to write for . The dot means “the derivative with respect to time”.
Today, the focus will be on linear dynamics, where is a linear function of . In Tutorial 1, we will:
Explore and understand the behavior of such systems where is a single variable
Consider cases where is a state vector representing two variables.
Setup¶
Section 1: One-dimensional Differential Equations¶
Video 1: Linear Dynamical Systems
Video available at https://
Video available at https://
This video serves as an introduction to dynamical systems as the mathematics of things that change in time, including examples of relevant timescales relevant for neuroscience. It covers the definition of a linear system and why we are spending a whole day on linear dynamical systems, and walks through solutions to one-dimensional, deterministic dynamical systems, their behaviors, and stability criteria.
Note that this section is a recap of Tutorials 2 and 3 of our pre-course calculus day.
Click here for text recap of video
Let’s start by reminding ourselves of a one-dimensional differential equation in x of the form
where a is a scalar.
Solutions for how x evolves in time when its dynamics are governed by such a differential equation take the form
\begin{equation}
x(t) = x_0\exp(a t)
\end{equation}where x_0 is the initial condition of the equation -- that is, the value of x at time 0.
To gain further intuition, let’s explore the behavior of such systems with a simple simulation. We can simulate an ordinary differential equation by approximating or modeling time as a discrete list of time steps , such that . We can get the small change over a small duration of time from the definition of the differential:
So, at each time step , we compute a value of , , as the sum of the value of at the previous time step, and a small change :
This very simple integration scheme, known as forward Euler integration, works well if is small and the ordinary differential equation is simple. It can run into issues when the ordinary differential equation is very noisy or when the dynamics include sudden big changes of . Such big jumps can occur, for example, in models of excitable neurons. In such cases, one needs to choose an integration scheme carefully. However, for our simple system, the simple integration scheme should work just fine!
Coding Exercise 1: Forward Euler Integration¶
Referred to as Exercise 1B in video
In this exercise, we will complete a function, integrate_exponential, to compute the solution of the differential equation using forward Euler integration. We will then plot this solution over time.
Interactive Demo 1: Forward Euler Integration¶
What happens when you change ? Try values where and .
The is the step size of the forward Euler integration. Try and increase . What happens to the numerical solution when you increase ?
Section 2: Oscillatory Dynamics¶
Estimated timing to here from start of tutorial: 20 min
Video 2: Oscillatory Solutions
Video available at https://
Video available at https://
We will now explore what happens when is a complex number and has a non-zero imaginary component.
Interactive Demo 2: Oscillatory Dynamics¶
Referred to as exercise 1B in video
In the following demo, you can change the real part and imaginary part of (so a = real + imaginary i)
What values of produce dynamics that both oscillate and grow?
What value of is needed to produce a stable oscillation of 0.5 Hertz (cycles/time units)?
Section 3: Deterministic Linear Dynamics in Two Dimensions¶
Estimated timing to here from start of tutorial: 33 min
Video 3: Multi-Dimensional Dynamics
Video available at https://
Video available at https://
This video serves as an introduction to two-dimensional, deterministic dynamical systems written as a vector-matrix equation. It covers stream plots and how to connect phase portraits with the eigenvalues and eigenvectors of the transition matrix A.
Click here for text recap of relevant part of video
Adding one additional variable (or dimension) adds more variety of behaviors. Additional variables are useful in modeling the dynamics of more complex systems with richer behaviors, such as systems of multiple neurons. We can write such a system using two linear ordinary differential equations:
\begin{aligned}
\dot{x}_1 &= {a}_{11} x_1 \\
\dot{x}_2 &= {a}_{22} x_2
\end{aligned}So far, this system consists of two variables (e.g. neurons) in isolation. To make things interesting, we can add interaction terms:
\begin{aligned}
\dot{x}_1 &= {a}_{11} x_1 + {a}_{12} x_2 \\
\dot{x}_2 &= {a}_{21} x_1 + {a}_{22} x_2
\end{aligned}We can write the two equations that describe our system as one (vector-valued) linear ordinary differential equation:
For two-dimensional systems, \mathbf{x} is a vector with 2 elements (x_1 and x_2) and \mathbf{A} is a 2 \times 2 matrix with \mathbf{A}=\begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix}.
Coding Exercise 3: Sample trajectories in 2 dimensions¶
Referred to in video as step 1 and 2 of exercise 1C
We want to simulate some trajectories of a given system and plot how and evolve in time. We will begin with this example system:
We will use an integrator from scipy, so we won’t have to solve the system ourselves. We have a helper function, plot_trajectory, that plots these trajectories given a system function. In this exercise, we will write the system function for a linear system with two variables.
Interactive Demo 3A: Varying A¶
We will now use the function we created in the last exercise to plot trajectories with different options for A. What kinds of qualitatively different dynamics do you observe?
Hint: Keep an eye on the x-axis and y-axis!
Interactive Demo 3B: Varying Initial Conditions¶
We will now vary the initial conditions for a given :
What kinds of qualitatively different dynamics do you observe? Hint: Keep an eye on the x-axis and y-axis!
Section 4: Stream Plots¶
Estimated timing to here from start of tutorial: 45 min
It’s a bit tedious to plot trajectories one initial condition at a time!
Fortunately, to get an overview of how a grid of initial conditions affect trajectories of a system, we can use a stream plot.
We can think of a initial condition as coordinates for a position in a space. For a 2x2 matrix , a stream plot computes at each position a small arrow that indicates and then connects the small arrows to form stream lines. Remember from the beginning of this tutorial that is the rate of change of . So the stream lines indicate how a system changes. If you are interested in a particular initial condition , just find the corresponding position in the stream plot. The stream line that goes through that point in the stream plot indicates .
Think! 4: Interpreting Eigenvalues and Eigenvectors¶
Using some helper functions, we show the stream plots for each option of A that you examined in the earlier interactive demo. We included the eigenvectors of as a red line (1st eigenvalue) and a blue line (2nd eigenvalue) in the stream plots.
What is special about the direction in which the principal eigenvector points? And how does the stability of the system relate to the corresponding eigenvalues? (Hint: Remember from your introduction to linear algebra that, for matrices with real eigenvalues, the eigenvectors indicate the lines on which is parallel to and real eigenvalues indicate the factor by which is stretched or shrunk compared to .)
Summary¶
Estimated timing of tutorial: 1 hour
In this tutorial, we learned:
How to simulate the trajectory of a dynamical system specified by a differential equation using a forward Euler integration scheme.
The behavior of a one-dimensional linear dynamical system is determined by , which may be a complex valued number. Knowing , we know about the stability and oscillatory dynamics of the system.
The dynamics of high-dimensional linear dynamical systems can be understood using the same intuitions, where we can summarize the behavior of the trajectories using the eigenvalues and eigenvectors of .