!pip install git+https://github.com/CU-Denver-MathStats-OER/ODEs
from IPython.display import clear_output
clear_output()
Python Tutorial for Phase Planes
This notebook demonstrates the use of functions related to phase plane portraits from the ode_tools
Python module.
- The actual Python code for each function can be found in the file named ode_tools.py located in the directory ../utils (or wherevever you have previously saved this file).
- Good news! The functions defined in ode_plot_tools.py are coded and ready for use with no mofications needed to the source file!
- You do not even have to view the source file to understand how to use and adjust the functions to fit your needs.
- See the documentation below for a “Quick Reference Guide” to working with phase plane portrait functions in the
ode_tools
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.
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}\]
Input vectors of values for the dependent variables
x
andy
.- 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\).
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
.
- In the code cell below, we enter the system of differential equations above.
import numpy as np
# Set plot range
= np.linspace(0.0, 5.0, 20) # range of values for x
x = np.linspace(0.0, 5.0, 20) # range of values for y
y
# Enter differential equation
def f(Y, t):
= Y
x, y return [3*x - 1.4*x*y, # formula for dx/dt
-y + 0.8*x*y] # formula for dy/dt
- 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: Loadingode_tools
from GitHub
from utils.ode_tools import phase_portrait # Only need to import one time.
- 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
, andf(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}\]
Input vectors of values for the dependent variables
x
andy
.- 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\).
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
.
- In the code cell below, we enter the system of differential equations above.
Enter the range of time over which the solution will be plotted using
tspan
.Enter the initial value for \(x\) at \(t=0\) as
x0
.Enter the initial value for \(y\) at \(t=0\) as
y0
.
import numpy as np
# Set plot range
= np.linspace(0.0, 5.0, 20) # range of values for x
x = np.linspace(0.0, 5.0, 20) # range of values for y
y
# Enter differential equation
def f(Y, t):
= Y
x, 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
= np.linspace(0, 50, 200) # range of time to visualize solution
tspan
# Enter initial values
= 2 # initial value of x
x0 = 3 # initial value of y y0
- 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: Loadingode_tools
from GitHub
from utils.ode_tools import plot_phase_sol # Only need to import one time.
- 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
, andy0
as needed. - Then run the command
plot_phase_sol(x, y, f, tspan, x0, y0)
.
Creative Commons License Information
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.