Python Tutorial for Phase Planes

Open In Colab


This notebook demonstrates the use of functions related to phase plane portraits from the ode_tools Python module.

Section 1: Loading ode_tools from GitHub


  • Run the code cell below to load the most up to date modules stored in GitHub.
  • You will only need to run this code cell one time during an active session.
!pip install git+https://github.com/CU-Denver-MathStats-OER/ODEs
from IPython.display import clear_output
clear_output()

Section 2: Plotting a Phase Plane Portrait


A function named phase_portrait() plots a phase plane portrait (vector field) for a system of first order differential equations over a specified range of values for the dependent variables \(x\) and \(y\).

  • We use x for dependent variable 1.
  • We use y for dependent variable 2.
  • We use the function f(Y, t) for the system of differential equations.
    • The output of this function is a vector.
    • The first entry is a formula for the first differential equation \(\dfrac{dx}{dt}\).
    • The second entry is a formula for the second differential equation \(\dfrac{dy}{dt}\).

How to Plot with phase_portrait(x, y, f)


Consider plotting the system the of differential equations below in the phase plane with a window of \(0 \leq x \leq 5\) and \(0 \leq y \leq 5\).

\[\begin{array}{l} \dfrac{dx}{dt} = 3x-1.4xy \\ \dfrac{dy}{dt} = -y + 0.8xy\\ \end{array}\]

  1. Input vectors of values for the dependent variables x and y.

    • In the code cell below, we set up a grid of equally spaced values along the intervals \(0 \leq x \leq 5\) and \(0 \leq y \leq 5\).
  2. Define the system of two differential equations f(Y,t).

    • In the code cell below, we enter the system of differential equations above.
    • The formula for \(\frac{dx}{dt}=3x-1.4xy\) is entered as 3*x - 1.4*x*y.
    • The formula for \(\frac{dy}{dt}=-y+0.8xy\) is entered as -y + 0.8*x*y.
import numpy as np

# Set plot range
x = np.linspace(0.0, 5.0, 20)  # range of values for x
y = np.linspace(0.0, 5.0, 20)  # range of values for y

# Enter differential equation
def f(Y, t):
    x, y = Y
    return [3*x - 1.4*x*y,  # formula for dx/dt
            -y + 0.8*x*y]  # formula for dy/dt
  1. Import the phase_portrait function.
  • Like packages, you only need to import a function one time after opening a notebook.
  • Be sure you have first loaded ode_tools.py from GitHub..
  • If you get an error message, it is likely you forgot to first load the ode_tools module from GitHub. See Section 1: Loading ode_tools from GitHub
from utils.ode_tools import phase_portrait  # Only need to import one time.
  1. We generate the slope field by running the function with the command phase_portrait(x, y, f).
# Plots a phase portrait

phase_portrait(x, y, f)

Each time you want to plot a new solution:


  • Redefine x, y, and f(Y, t) as needed.
  • Then run the command phase_portrait(x, y, f).

Section 3: Adding a Solution to a Phase Plane Portrait


A function named plot_phase_sol plots particular solutions in a phase plane portrait (vector field) for a system of first order differential equations over a specified range of values for the indpendent and dependent variables.

  • We use x for dependent variable 1.
  • We use y for dependent variable 2.
  • We use the function f(Y, t) for the system of differential equations.
    • The output of this function is a vector.
    • The first entry is a formula for the first differential equation \(\dfrac{dx}{dt}\).
    • The second entry is a formula for the second differential equation \(\dfrac{dy}{dt}\).
  • We use tspan for the range of time to visualize the solution.
  • We use x0 for the initial value of the first dependent variable \(x\).
  • We use y0 for the initial value of the first dependent variable \(y\).

How to Plot with plot_phase_sol(x, y, f, tspan, x0, y0)


Consider plotting the solution to the system the of differential equations below with initial condition \((x(0),y(0)) = (2,3)\) in the phase plane with over a window of \(0 \leq x \leq 5\) and \(0 \leq y \leq 5\).

\[\begin{array}{l} \dfrac{dx}{dt} = 3x-1.4xy \\ \dfrac{dy}{dt} = -y + 0.8xy\\ \end{array}\]

  1. Input vectors of values for the dependent variables x and y.

    • In the code cell below, we set up a grid of equally spaced values along the intervals \(0 \leq x \leq 5\) and \(0 \leq y \leq 5\).
  2. Define the system of two differential equations f(Y,t).

    • In the code cell below, we enter the system of differential equations above.
    • The formula for \(\frac{dx}{dt}=3x-1.4xy\) is entered as 3*x - 1.4*x*y.
    • The formula for \(\frac{dy}{dt}=-y+0.8xy\) is entered as -y + 0.8*x*y.
  3. Enter the range of time over which the solution will be plotted using tspan.

  4. Enter the initial value for \(x\) at \(t=0\) as x0.

  5. Enter the initial value for \(y\) at \(t=0\) as y0.

import numpy as np

# Set plot range
x = np.linspace(0.0, 5.0, 20)  # range of values for x
y = np.linspace(0.0, 5.0, 20)  # range of values for y

# Enter differential equation
def f(Y, t):
    x, y = Y
    return [3*x - 1.4*x*y,  # formula for dx/dt
            -y + 0.8*x*y]  # formula for dy/dt

# Enter range of time
tspan = np.linspace(0, 50, 200) # range of time to visualize solution

# Enter initial values
x0 = 2  # initial value of x
y0 = 3  # initial value of y
  1. Import the plot_phase_sol function.
  • Like packages, you only need to import a function one time after opening a notebook.
  • Be sure you have first loaded ode_tools.py from GitHub..
  • If you get an error message, it is likely you forgot to first load the ode_tools module from GitHub. See Section 1: Loading ode_tools from GitHub
from utils.ode_tools import plot_phase_sol  # Only need to import one time.
  1. We generate the slope field by running the function with the command plot_phase_sol(x, y, f, tspan, x0, y0).
# Plots a solution in a phase plane portrait

plot_phase_sol(x, y, f, tspan, x0, y0)

Each Time You Want To Plot A New Solution:


  • Redefine x, y, f(Y, t), tspan, x0, and y0 as needed.
  • Then run the command plot_phase_sol(x, y, f, tspan, x0, y0).

Creative Commons License Information


Creative Commons License
Exploring Differential Equations by Adam Spiegler is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Python scripts created by Jonathon Hirschi, Troy Butler, and Adam Spiegler.