!pip install git+https://github.com/CU-Denver-MathStats-OER/ODEs
from IPython.display import clear_output
clear_output()Python Tutorial for First Order ODEs
This notebook demonstrates the use of 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.
- 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 functions in the
ode_plot_toolsmodule.
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 Slope Fields with slope_field(t, x, diffeq)
A function named slope_field() plots a slope field for a differential equation over a specified range of values for the indpendent and dependent variables.
- We use
tfor the independent variable. - We use
xfor the dependent variable. - We use
diffeqfor the differential equation.
How to Plot with slope_field(t, x, diffeq)
- Input vectors of values for
tandx(points where the vectors will be plotted) and definediffeq.
import numpy as np
# Setup the grid
t = np.linspace(0,10,11) # np.linspace(initial, end, number_values)
x = np.linspace(40,60,10) # np.linspace(initial, end, number_values)
# Setup the differential equation
def diffeq(t, x):
return 9.8 - (x / 5) - We import the
slope_field()function.
- Like packages, you only need to import a function one time after opening a notebook.
- Be sure you have first loaded the ode_tools module from GitHub..
- Refer to Section 1: Loading
ode_toolsfrom GitHub to correct the error.
- Refer to Section 1: Loading
from utils.ode_tools import slope_field # Only need to run one time- We generate the slope field by running the function with the command
slope_field(t, x, diffeq).
# inputs t, x, and diffeq defined above
slope_field(t, x, diffeq) # Plot slope field of diffeqEach time you want to plot a new slope field:
- Redefine
t,x, and/ordiffeqas needed. - Then run the command
slope_field(t, x, diffeq).
Section 3: Plotting Solutions to Initial Value Problems with plot_sol()
A function named plot_sol(t, x, diffeq, t0, x0) plots a slope field as well as the solution that passes thru a given initial condition.
- We use
tfor the independent variable. - We use
xfor the dependent variable. - We use
diffeqfor the differential equation. - We use
t0to denote the initial value of the input. - We use
x0to denote the output at the initial valuet0.
How to Plot with plot_sol(t, x, diffeq, t0, x0)
- Specifying the inputs for
plot_sol(t, x, diffeq, t0, x0)
import numpy as np
# Setup the grid
t = np.linspace(0,10,11) # np.linspace(initial, end, number_values)
x = np.linspace(40,60,10) # np.linspace(initial, end, number_values)
# Setup the differential equation
def diffeq(t, x):
return 9.8 - (x / 5)
# Enter t0, the initial value of t
# Enter x0, the value of x at t0
t0 = 2
x0 = 55- Import the
plot_sol()function.
- Like packages, you only need to import a function one time after opening a notebook.
- Be sure you have first loaded the ode_tools module from GitHub..
- Refer to Section 1: Loading
ode_toolsfrom GitHub to correct the error.
- Refer to Section 1: Loading
from utils.ode_tools import plot_sol # Import function- We generate the slope field by running the function with the command
plot_sol(t, x, diffeq, t0, x0).
# t, x, diffeq, and x0 have been defined above
plot_sol(t, x, diffeq, t0, x0) # Plot solution with initial conditionEach time you want to plot a new solution:
- Redefine
t,x,diffeq,t0, and/orx0as needed. - Then run the command
plot_sol(t, x, diffeq, t0, x0).
Section 4: Euler’s Method with euler_method()
A function named euler_method() computes \(n\) steps of Euler’s method each with identical step size \(\Delta t = dt\).
- We use
diffeqfor the differential equation. - We use
t0for the initial value of the independent variable. - We use
x0for corresponding value of the dependent variable at timet0. - We use
dtto denote the step size \(\Delta t\). - We use
nto denote the number of steps \(n\).
How to Approximate with euler_method(diffeq, t0, x0, dt, n)
Define
diffeq.Define the initial value \((t_0, x_0) =\) (
t0,x0).Define the step size \(\Delta t\) as
dt, and the number of stepsn.
# Define diffeq
def diffeq(t, x): # t is independent variable and x is dependent variable
return x + t # Use t and x for ind and dep variables
# Initial value
t0 = 0 # initial value of input
x0 = 4 # initial value output when t = t_0
dt = 0.5 # Step size
n = 3 # number of steps- We import the
euler_method()function.
- Like packages, you only need to import a function one time after opening a notebook.
- Be sure you have first loaded the ode_tools module from GitHub..
- Refer to Section 1: Loading
ode_toolsfrom GitHub to correct the error.
- Refer to Section 1: Loading
from utils.ode_tools import euler_method- Calculate each step with the function
euler_method(diffeq, t0, x0, dt, n).
# diffeq, t0, x0, dt, and n have been defined above
euler_method(diffeq, t0, x0, dt, n) # Apply Euler's methodEach time you want to apply Euler’s method:
- Redefine
diffeq,t0,x0,dt, and/ornas needed. - Then run the command
euler_method(diffeq, t0, x0, dt, n).
Section 5: Visualizing Euler’s Method with plot_euler()
A function named plot_method() graphically compares numerical approximations from Euler’s method with the actual solutions.
- We use
tfor the independent variable. - We use
xfor the dependent variable. - We use
diffeqfor the differential equation. - We use
t0for the initial value of the independent variable. - We use
x0to denote the output at the initial value oft. - We use
dtto denote the step size \(\Delta t\). - We use
nto denote the number of steps \(n\).
How to Use plot_euler(t, x, diffeq, t0, x0, dt, n)
Input vectors of values for
tandx(points where the vectors will be plotted) and definediffeq.Define an initial condition \((t_0, x_0) =\)(
t0,x0).Define the step size \(\Delta t=\)
dt, and number of iterations \(n=\)n.
import numpy as np
# Set up gride for slope field
t = np.linspace(0, 1.5, 7)
x = np.linspace(0, 20, 21)
# Define differential equation
def diffeq(t, x):
return x + t
# Define initial value
t0 = 0 # initial input value
x0 = 4 # initial output value
# Define step size and n
dt = 0.5 # step size
n = 3 # number of steps- We import the
plot_euler()function.
- Like packages, you only need to import a function one time after opening a notebook.
- Be sure you have first loaded the ode_tools module from GitHub..
- Refer to Section 1: Loading
ode_toolsfrom GitHub to correct the error.
- Refer to Section 1: Loading
# Import plot_euler function from ode_plot_tools module.
from utils.ode_tools import plot_euler- Generate the plot by running the function with the command
plot_euler(t, x, diffeq, t0, x0, dt, n).
# t, x, diffeq, t0, x0, dt, and n have been defined above
plot_euler(t, x, diffeq, t0, x0, dt, n) # create plot of euler approxEach time you want to Create a New Plot with plot_euler():
- Redefine
t,x,diffeq,t0,x0,dt, and/ornas needed. - Then run the command
plot_euler(t, x, diffeq, t0, x0, dt, n).
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.