Python Tutorial for First Order ODEs

Open In Colab


This notebook demonstrates the use of 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 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)


  1. Input vectors of values for t and x (points where the vectors will be plotted) and define diffeq.
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) 
  1. 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..
from utils.ode_tools import slope_field  # Only need to run one time
  1. 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 diffeq

Each time you want to plot a new slope field:


  • Redefine t, x, and/or diffeq 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 value t0.

How to Plot with plot_sol(t, x, diffeq, t0, x0)


  1. 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
  1. 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..
from utils.ode_tools import plot_sol  # Import function
  1. 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 condition

Each time you want to plot a new solution:


  • Redefine t, x, diffeq, t0, and/or x0 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 time t0.
  • 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)


  1. Define diffeq.

  2. Define the initial value \((t_0, x_0) =\) (t0, x0).

  3. Define the step size \(\Delta t\) as dt, and the number of steps n.

# 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
  1. 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..
from utils.ode_tools import euler_method
  1. 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 method

Each time you want to apply Euler’s method:


  • Redefine diffeq, t0, x0, dt, and/or n 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 of t.
  • 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)


  1. Input vectors of values for t and x (points where the vectors will be plotted) and define diffeq.

  2. Define an initial condition \((t_0, x_0) =\)(t0, x0).

  3. 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
  1. 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..
# Import plot_euler function from ode_plot_tools module.

from utils.ode_tools import plot_euler
  1. 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 approx

Each time you want to Create a New Plot with plot_euler():


  • Redefine t, x, diffeq, t0, x0, dt, and/or n as needed.
  • Then run the command plot_euler(t, x, diffeq, t0, x0, dt, n).

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.