!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_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 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
t
for the independent variable. - We use
x
for the dependent variable. - We use
diffeq
for the differential equation.
How to Plot with slope_field(t, x, diffeq)
- Input vectors of values for
t
andx
(points where the vectors will be plotted) and definediffeq
.
import numpy as np
# Setup the grid
= np.linspace(0,10,11) # np.linspace(initial, end, number_values)
t = np.linspace(40,60,10) # np.linspace(initial, end, number_values)
x
# 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_tools
from 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
# Plot slope field of diffeq slope_field(t, x, diffeq)
Each time you want to plot a new slope field:
- Redefine
t
,x
, and/ordiffeq
as 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
t
for the independent variable. - We use
x
for the dependent variable. - We use
diffeq
for the differential equation. - We use
t0
to denote the initial value of the input. - We use
x0
to 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
= np.linspace(0,10,11) # np.linspace(initial, end, number_values)
t = np.linspace(40,60,10) # np.linspace(initial, end, number_values)
x
# 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
= 2
t0 = 55 x0
- 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_tools
from 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 solution with initial condition plot_sol(t, x, diffeq, t0, x0)
Each time you want to plot a new solution:
- Redefine
t
,x
,diffeq
,t0
, and/orx0
as 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
diffeq
for the differential equation. - We use
t0
for the initial value of the independent variable. - We use
x0
for corresponding value of the dependent variable at timet0
. - We use
dt
to denote the step size \(\Delta t\). - We use
n
to 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
= 0 # initial value of input
t0 = 4 # initial value output when t = t_0
x0
= 0.5 # Step size
dt = 3 # number of steps n
- 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_tools
from 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
# Apply Euler's method euler_method(diffeq, t0, x0, dt, n)
Each time you want to apply Euler’s method:
- Redefine
diffeq
,t0
,x0
,dt
, and/orn
as 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
t
for the independent variable. - We use
x
for the dependent variable. - We use
diffeq
for the differential equation. - We use
t0
for the initial value of the independent variable. - We use
x0
to denote the output at the initial value oft
. - We use
dt
to denote the step size \(\Delta t\). - We use
n
to denote the number of steps \(n\).
How to Use plot_euler(t, x, diffeq, t0, x0, dt, n)
Input vectors of values for
t
andx
(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
= np.linspace(0, 1.5, 7)
t = np.linspace(0, 20, 21)
x
# Define differential equation
def diffeq(t, x):
return x + t
# Define initial value
= 0 # initial input value
t0 = 4 # initial output value
x0
# Define step size and n
= 0.5 # step size
dt = 3 # number of steps n
- 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_tools
from 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
# create plot of euler approx plot_euler(t, x, diffeq, t0, x0, dt, n)
Each time you want to Create a New Plot with plot_euler()
:
- Redefine
t
,x
,diffeq
,t0
,x0
,dt
, and/orn
as 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.