Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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 xx. One typically describes this rate of change using the derivative of xx with respect to time (dx/dtdx/dt) on the left hand side of the differential equation:

dxdt=f(x)\frac{dx}{dt} = f(x)

A common notational short-hand is to write x˙\dot{x} for dxdt\frac{dx}{dt}. The dot means “the derivative with respect to time”.

Today, the focus will be on linear dynamics, where f(x)f(x) is a linear function of xx. In Tutorial 1, we will:

Source

Setup

Source
Source
Source

Section 1: One-dimensional Differential Equations

Source
Source

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

\dot{x} = a x

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 t0,t1,t2,…t_0, t_1, t_2, \dots, such that ti+1=ti+dtt_{i+1}=t_i+dt. We can get the small change dxdx over a small duration dtdt of time from the definition of the differential:

x˙=dxdtdx=x˙ dt\begin{aligned} \dot x &= \frac{dx}{dt} \\ dx &= \dot x\, dt \end{aligned}

So, at each time step tit_i, we compute a value of xx, x(ti)x(t_i), as the sum of the value of xx at the previous time step, x(ti−1)x(t_{i-1}) and a small change dx=x˙ dtdx=\dot x\,dt:

x(ti)=x(ti−1)+x˙(ti−1)dtx(t_i)=x(t_{i-1})+\dot x(t_{i-1}) dt

This very simple integration scheme, known as forward Euler integration, works well if dtdt 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 xx. 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 x˙=ax\dot{x} = a x using forward Euler integration. We will then plot this solution over time.

Source
Source

Interactive Demo 1: Forward Euler Integration

  1. What happens when you change aa? Try values where a<0a<0 and a>0a>0.

  2. The dtdt is the step size of the forward Euler integration. Try a=−1.5a = -1.5 and increase dtdt. What happens to the numerical solution when you increase dtdt?

Source
Source
Source

Section 2: Oscillatory Dynamics

Estimated timing to here from start of tutorial: 20 min

Source
Source

We will now explore what happens when aa 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 aa (so a = real + imaginary i)

  1. What values of aa produce dynamics that both oscillate and grow?

  2. What value of aa is needed to produce a stable oscillation of 0.5 Hertz (cycles/time units)?

Source
Source
Source

Section 3: Deterministic Linear Dynamics in Two Dimensions

Estimated timing to here from start of tutorial: 33 min

Source
Source

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:

\dot{\mathbf{x}} = \mathbf{A} \mathbf{x}

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 x1x_1 and x2x_2 evolve in time. We will begin with this example system:

x˙=[2−51−2]x\dot{\mathbf{x}} = \begin{bmatrix} 2 & -5 \\ 1 & -2 \end{bmatrix} \mathbf{x}

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.

Source
Source

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!

Source
Source
Source

Interactive Demo 3B: Varying Initial Conditions

We will now vary the initial conditions for a given A\mathbf{A}:

x˙=[2−51−2]x\dot{\mathbf{x}} = \begin{bmatrix} 2 & -5 \\ 1 & -2 \end{bmatrix} \mathbf{x}

What kinds of qualitatively different dynamics do you observe? Hint: Keep an eye on the x-axis and y-axis!

Source
Source
Source

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 x0=(x10,x20){\bf x}_0=(x_{1_0},x_{2_0}) as coordinates for a position in a space. For a 2x2 matrix A\bf A, a stream plot computes at each position x\bf x a small arrow that indicates Ax\bf Ax and then connects the small arrows to form stream lines. Remember from the beginning of this tutorial that x˙=Ax\dot {\bf x} = \bf Ax is the rate of change of x\bf x. So the stream lines indicate how a system changes. If you are interested in a particular initial condition x0{\bf x}_0, just find the corresponding position in the stream plot. The stream line that goes through that point in the stream plot indicates x(t){\bf x}(t).

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 A\bf A 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 Ax\bf Ax is parallel to x\bf x and real eigenvalues indicate the factor by which Ax\bf Ax is stretched or shrunk compared to x\bf x.)

Source
Source
Source

Summary

Estimated timing of tutorial: 1 hour

In this tutorial, we learned: