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.

Open In Colab Open In Kaggle

This is the scrollytelling version of Tutorial 1: the same content, laid out as a pinned stage of figures, videos and live Python next to text cards that scroll past. Compare the two to decide which layout suits your material.

neuromatch academy

Tutorial 2: Linear dynamical systems

Week 2, Day 3: Linear Systems

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

In this tutorial, we will

  1. Explore and understand the behavior of such systems where xx is a single variable

  2. Consider cases where x\mathbf{x} is a state vector representing two variables

Tutorial slides

Download the slides

Video 1: Linear Dynamical Systems

YouTube
Bilibili

Video available at https://youtube.com/watch?v=87z6OR7-DBI

Video available at https://www.bilibili.com/video/BV1up4y1S7wj

Video 2: Oscillatory Solutions

YouTube
Bilibili

Video available at https://youtube.com/watch?v=vPYQPI4nKT8

Video available at https://www.bilibili.com/video/BV1gZ4y1u7PK

Video 3: Multi-Dimensional Dynamics

YouTube
Bilibili

Video available at https://youtube.com/watch?v=c_GdNS3YH_M

Video available at https://www.bilibili.com/video/BV1pf4y1R7uy

x˙1=a11x1+a12x2x˙2=a21x1+a22x2\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}
x˙=Ax,A=[a11a12a21a22]\dot{\mathbf{x}} = \mathbf{A} \mathbf{x}, \qquad \mathbf{A}=\begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix}

Summary

What we learned

  • How to simulate the trajectory of a dynamical system specified by a differential equation x˙=f(x)\dot{x} = f(x) using a forward Euler integration scheme.

  • The behavior of a one-dimensional linear dynamical system x˙=ax\dot{x} = a x is determined by aa, which may be a complex valued number. Knowing aa, we know about the stability and oscillatory dynamics of the system.

  • The dynamics of high-dimensional linear dynamical systems x˙=Ax\dot{\mathbf{x}} = \mathbf{A} \mathbf{x} can be understood using the same intuitions, where we can summarize the behavior of the trajectories using the eigenvalues and eigenvectors of A\mathbf{A}.

Tutorial objectives

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 express the rate of change of the state variable xx, written as the derivative x˙=dx/dt\dot{x} = dx/dt:

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

Today, the focus is on linear dynamics, where f(x)f(x) is a linear function of xx.

Slides

Tutorial slides

Here are the slides for the whole tutorial. You can also download them.

Section 1

Section 1: One-dimensional differential equations

This video introduces dynamical systems as the mathematics of things that change in time, including timescales relevant for neuroscience. It covers the definition of a linear system and walks through solutions to one-dimensional, deterministic dynamical systems, their behaviors, and stability criteria.

This section is a recap of Tutorials 2 and 3 of the pre-course calculus day.

Recap

The exponential solution

A one-dimensional differential equation in xx of the form

x˙=ax\dot{x} = a x

where aa is a scalar, has solutions

x(t)=x0exp⁡(at)x(t) = x_0\exp(a t)

where x0x_0 is the initial condition: the value of xx at time 0. The sign of aa decides whether xx decays, stays put, or grows.

Simulation

Forward Euler integration

We can simulate an ODE by treating time as a list of steps t0,t1,t2,…t_0, t_1, t_2, \dots with ti+1=ti+dtt_{i+1}=t_i+dt. From the definition of the derivative, the small change over dtdt is

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

So at each step we add that small change to the previous value:

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

This scheme, forward Euler integration, works well when dtdt is small and the equation is simple. It struggles with noisy dynamics or sudden big jumps in xx (as in models of excitable neurons). For our system it works just fine.

Coding Exercise 1

Implement forward Euler

Referred to as Exercise 1B in video

Complete integrate_exponential to compute the solution of x˙=ax\dot{x} = a x with forward Euler integration, then run it to plot the solution over time.

If you skip ahead, the cells after this one use the reference solution, so they still work.

Result

Euler against the exact solution

With a small step, forward Euler sits on top of the exact solution x0eatx_0 e^{at}. With bigger steps, it becomes choppy and drifts away from it.

Interactive Demo 1

Forward Euler integration

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

  2. 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?

Section 2

Section 2: Oscillatory dynamics

Estimated timing to here from start of tutorial: 20 min

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

Change the real and imaginary parts of aa (so aa = real + imaginary ii).

  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)?

Section 3

Section 3: Deterministic linear dynamics in two dimensions

Estimated timing to here from start of tutorial: 33 min

This video introduces 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.

Recap

Two variables

Adding a second variable (or dimension) adds more variety of behaviors, for example in systems of several neurons. Two variables in isolation evolve as

x˙1=a11x1x˙2=a22x2\begin{aligned} \dot{x}_1 &= {a}_{11} x_1 \\ \dot{x}_2 &= {a}_{22} x_2 \end{aligned}

To make things interesting, we add interaction terms a12x2{a}_{12} x_2 and a21x1{a}_{21} x_1. The two equations then become one vector-valued linear ODE, where x\mathbf{x} has two elements and A\mathbf{A} is a 2×22 \times 2 matrix.

Coding Exercise 3

Sample trajectories in 2 dimensions

Referred to in video as step 1 and 2 of exercise 1C

We simulate trajectories of the system

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

and plot how x1x_1 and x2x_2 evolve in time. An integrator from scipy does the solving, and the helper plot_trajectory draws the result. Write the system function for a linear system with two variables.

Interactive Demo 3A

Varying A

Pick different options for A\mathbf{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

Now vary the initial conditions for a fixed 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!

Section 4

Section 4: Stream plots

Estimated timing to here from start of tutorial: 45 min

Plotting trajectories one initial condition at a time is tedious. A stream plot shows how a whole grid of initial conditions evolves.

Think of an initial condition x0=(x10,x20){\bf x}_0=(x_{1_0},x_{2_0}) as a position in a space. At each position x\bf x, a stream plot draws a small arrow for Ax\bf Ax and connects the arrows into stream lines. Since x˙=Ax\dot {\bf x} = \bf Ax, the stream line through x0{\bf x}_0 shows x(t){\bf x}(t).

Think! 4

Interpreting eigenvalues and eigenvectors

Here are the stream plots for each option of A from Demo 3A, with the eigenvectors of A\bf A as a red line (1st eigenvalue) and a blue line (2nd eigenvalue).

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: from your introduction to linear algebra, for matrices with real eigenvalues, the eigenvectors are the lines on which Ax\bf Ax is parallel to x\bf x, and the eigenvalues are the factors by which Ax\bf Ax is stretched or shrunk compared to x\bf x.)