1.4: Numerical Approximations

Open In Colab

Reading: Notes on Diffy Q’s Section 1.7

A Rate of Change Equation for Limited Resources


In a previous question we saw that the rate of change equation \(\dfrac{dP}{dt}=0.3P\) can be used to model a situation where there is one species, continuous reproduction, and unlimited resources. In most situations, however, the resources are not unlimited, so to improve the model one has to modify the rate of change equation \(\dfrac{dP}{dt}=0.3P\) to account for the fact that resources are limited.

Question 1:


Consider a modified rate of change equation for the population given by

\[\color{dodgerblue}{\dfrac{dP}{dt}=0.3P\left(1-\dfrac{P}{10}\right)}\]

and answer the questions below.

Question 1a:


In what ways does the modified rate of change equation account for limited resources? (Think of 10 as scaled to mean 10,000 or 100,000.)

Solution to Question 1a:







Question 1b:


How do you interpret the solution with initial condition \(P(0) = 10\)?

Solution to Question 1b:







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()

Question 2:


The Python code cell below imports the slope_field() function from the ode_tools module. Recall we introduced this function in Lab 1.2 Slope Fields. See ODE Tools Tutorial for a quick reference on how to use slope_field() and other functions for Chapter 1.

Question 2a:


Enter a formula for the differential equation

\[\color{dodgerblue}{\frac{dP}{dt}=0.3P\left(1-\frac{P}{10}\right)}\]

from Question 1 in place of the ?? in line of code where the formula for the differential equation should be entered. Then run the code to use the slope_field() function to plot the slope field for the differential equation. - Be sure you have already loaded ode_tools from GitHub. See either Loading from GitHub instructions above.


Solution to Question 2a:






Edit and run the code cell below.


import numpy as np
from utils.ode_tools import slope_field

# Define points where vectors will be plotted
t = np.linspace(0, 7, 8)  # Independent variable, np.linspace(initial, end, number_values)
p = np.linspace(-4, 15, 20)  # Dependent variable, np.linspace(initial, end, number_values)

# Define the differential equation
def diffeq(t, p):  # t is independent variable and p is dependent variable
    return ??  # enter the formula for dp/dt

# Run the slope field plotter
slope_field(t, p, diffeq)

Question 2b:


In what ways are your responses to Question 1 visible in the slope field you created in part 2a?

Solution to Question 2b:







Question 2c:


In this question, negative \(P\) values do not make sense, but we can still mathematically make sense of the slope field for negative \(P\) values. Explain why the slope field looks the way it does below the \(t\)-axis.

Solution to Question 2c:







Question 3:


Using the differential equation from from Question 1,

\[\color{dodgerblue}{\frac{dP}{dt}=0.3P\left(1-\frac{P}{10}\right)},\]

and in addition we know there are initially \(P(0)=2\) fish in the lake, approximately how many fish are in the lake at time \(t=2\)? How did you arrive at your approximation?

Tip

Initially \(\dfrac{dP}{dt} = 0.48\), but what meaning does \(0.48\) have? What are the units?

Solution to Question 3:







Question 4:


The Python code cell below imports the plot_sol() function from the module named ode_tools. Recall we introduced this function in Lab 1.2 Slope Fields. See ODE Tools Tutorial for a quick reference on how to use plot_sol() and other functions for Chapter 1.

Run the code cell below to use the plot_sol() function to check your previous answer by plotting the solution to the initial value problem

\[\color{dodgerblue}{\frac{dP}{dt}=0.3P\left(1-\frac{P}{10}\right), \qquad P(0)=2}.\]

Solution to Question 4:






Run the code cell below to check your previous answer.


from utils.ode_tools import plot_sol

# Note t, p, and diffeq have already been defined in Question 2
# Enter the initial values t0 and p0 

t0 = 0
p0 = 2

# Run the function to create a plot
plot_sol(t, p, diffeq, t0, p0)

An Iterative Process to Predict Future Fish Populations


Question 5:


The population of fish in a lake is can be modeled with the differential equation from Question 1

\[\color{dodgerblue}{\frac{dP}{dt}=0.3P\left(1-\frac{P}{10}\right)}.\]

Edit the code cell below to stitch together, in a tip to tail manner, several tangent vectors to produce a graph of the population versus time if at time \(t = 0\) we know there are 8 fish in the lake (again, think of 8 as scaled for say, 8000 or 80,000 fish).

Solution to Question 5:



  • The first part of the code cell below that creates the slope field does not need to be edited.
  • For each of the three commented out plt.quiver() commands:
    • There are two ?? in each of the commented out commands that need to be replaced.
    • Add a second vector where the tail of the first vector ends by replacing each ?? with an appropriate value.
    • After adding a second vector, repeat this process to add a third vector.
    • Repeat one more time to add a fourth vector.
    • You do not need to edit the code below beyond uncommenting out lines of code and replacing each ??’s.
  • Optional: Consider how to improve this method so the solution is more accurate.




import matplotlib.pyplot as plt

# DO NOT EDIT: Set up gride for slope field
t = np.linspace(0, 8, 9)  
p = np.linspace(-4, 15, 20)
T, P = np.meshgrid(t, p)

# DO NOT EDIT: Define Differential Equation
def diffeq(t, p):  # t is independent variable and p is dependent variable
    return 0.3 * p * (1 - p/10) # Use t and p for ind and dep variables
dp = diffeq(T, P)
dt = np.ones(dp.shape)

# DO NOT EDIT: normalize the line segments
dpu = dp / np.sqrt(dt**2 + dp**2)  # normalize dp
dtu = dt / np.sqrt(dt**2 + dp**2)  # normalize dt
    
# DO NOT EDIT: Plot Slope Field
plt.quiver(T, P, dtu, dpu,  # Plot a 2D field of arrows
            units = 'xy',  
            angles = 'xy')

#######################################
# STUDENT TO DO: Add your own vectors
# Replace each ?? with an appropriate value
#######################################
# Plot vectors
plt.quiver(0, 8, 2, 2*diffeq(0, 8), color = 'b', 
           angles = 'xy',
           scale = 1,
           scale_units = 'xy') 
#plt.quiver(2, ??, 2, 2*diffeq(2, ??), color = 'b', 
#           angles = 'xy',
#           scale = 1,
#           scale_units = 'x') 
#plt.quiver(4, ??, 2, 2*diffeq(4, ??), color = 'b', 
#           angles = 'xy',
#           scale = 1,
#           scale_units = 'xy') 
#plt.quiver(6, ??, 2, 2*diffeq(6, ??), color = 'b', 
#           angles = 'xy',
#           scale = 1,
#           scale_units = 'xy') 

plt.show()

Question 6:


Explain how you are thinking about rate of change in your method. For example, is the rate of change constant over some increment? If yes, over what increment? If no, is the rate of change always changing?

Solution to Question 6:







Question 7:


Using the differential equation

\[\color{dodgerblue}{\frac{dP}{dt}= P\left(1-\dfrac{P}{20}\right)}\]

and initial condition \(P(0) = 10\), José and Julie started the table given in the solution below to numerically keep track of their tip-to-tail method for connecting tangent vectors. Explain José’s and Julie’s approach and complete their table. Round to two decimal places.

Solution to Question 7:





Explain their approach:




Complete table below.

Tip

You may use Python to help with calculations.

\(\large t\) \(\large P\) \(\large \dfrac{dP}{dt}\)
0 10 5
0.5 12.5 ??
1.0 ?? ??
1.5 ??




Question 8:


Using the same differential equation and initial condition as José and Julie in Question 7,

\[\frac{dP}{dt}=P\left( 1-\frac{P}{20}\right) \quad \mbox{with} \quad P(0)=10,\]

Derrick and Delores started their table as shown in the solution below. Explain how Derrick and Delores’ approach is different from José and Julie’s and then complete their table. Round to two decimal places.

Solution to Question 8:






Explain how their approach is different:




Complete table below.

Tip

You may use Python to help with calculations.

\(t\) \(P\) \(\dfrac{dP}{dt}\)
0 10 5
0.25 11.25 ??
0.5 ?? ??
0.75 ??




#import matplotlib.pyplot as plt

# DO NOT EDIT: Set up grid for slope field
t = np.linspace(0, 3, 13)  
y = np.linspace(0, 16, 17)
T, Y = np.meshgrid(t, y)

# DO NOT EDIT: Define Differential Equation
def diffeq(t, y):  # t is independent variable and y is dependent variable
    return y + t # Use t and y for ind and dep variables
dy = diffeq(T, Y)
dt = np.ones(dy.shape)

# DO NOT EDIT: normalize the line segments
dyu = dy / np.sqrt(dt**2 + dy**2)  # normalize dy
dtu = dt / np.sqrt(dt**2 + dy**2)  # normalize dt
    
# DO NOT EDIT: Plot Slope Field
plt.quiver(T, Y, dtu, dyu,  # Plot a 2D field of arrows
            units = 'xy', 
            angles = 'xy')

#######################################
# STUDENT TO DO: Add your own vectors
# Replace each ?? with an appropriate value
#######################################
# Plot vectors
plt.quiver(0, 4, 0.5, 2 , color = 'b', 
           angles = 'xy',
           scale = 1,
           scale_units = 'xy') 
plt.quiver(0.5, ??, 0.5, ??, color = 'b', 
           angles = 'xy',
           scale = 1,
           scale_units = 'xy') 
plt.quiver(1, ??, 0.5, ??, color = 'b', 
           angles = 'xy',
           scale = 1,
           scale_units = 'xy') 

plt.show()

Question 9:


Consider the differential equation \(\color{dodgerblue}{\dfrac{dy}{dt}=y+t}\) and initial condition \(\color{dodgerblue}{y(0) = 4}\).

  1. Use José and Julie’s approach (using \(\Delta t = 0.5\)) to find \(y(1.5)\). You may find the table below useful for organizing your work.

  2. Edit the code cell below to plot your answer to part (a).

Solution to Question 9:



  1. Complete table below.
Tip

You may use Python to help with calculations.

\(t\) \(y\) \(\dfrac{dy}{dt}\) \(\Delta y\)
0 4 ?? ??
?? ?? ?? ??
?? ?? ?? ??
?? ?? ?? ??




  1. In the code cell below, you need to replace each ?? in the last portion of the code.
  • There are two ?? in lines of code that add the last two vectors to the plot.
  • Add a second vector where the tail of the first vector ends by replacing each ?? with appropriate values.
  • After adding a second vector, repeat this process to add a third vector.
  • You do not need to edit the code below beyond replacing each ??.

Question 10:


  1. Is your value for \(y(1.5)\) the exact value or an approximate value? Explain.


  1. Generalizing your tip-to-tail approach: Create an equation-based procedure/algorithm that would allow you to predict future \(y\)-values for any differential equation \(\dfrac{dy}{dt}\), any given initial condition, and any time increment.


Solution to Question 10:




a. Explain.

  1. Write a formula.




Comparing Tail to Tip Sketch with Actual Solution


Run the code below (no edits needed) to compare your approximation from Question 9 with the actual solution.

Note

The plot below is generated using the plot_euler() function is in the ode_tools module. See ODE Tools Tutorial for a quick reference on how to use plot_euler() and other functions for Chapter 1.

  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), step size \(\Delta t=\) dt, and number of iterations \(n=\) n.

  3. We import the plot_euler() function.

  4. We generate the plot by running the function with the command plot_euler(t, x, diffeq, t0, x0, dt, n).

# Import plot_euler function from utils.ode_tools module.
from utils.ode_tools import plot_euler

# Set up gride for slope field
t = np.linspace(0, 1.5, 7)  
y = np.linspace(0, 20, 21)

# Define differential equation
def diffeq(t, y):
    return y + t 

# Define initial value, step size, and number of steps
t0 = 0  # initial input value
y0 = 4  # initial output value
dt = 0.5  # step size
n = 3  # number of steps


plot_euler(t, y, diffeq, t0, y0, dt, n)

Writing Python Code for Euler’s Method


Question 11:


Consider the differential equation \(\color{dodgerblue}{\dfrac{dy}{dt}=y+t}\) and initial condition \(\color{dodgerblue}{y(0) = 4}\).

Using steps each size \(\Delta t = 0.5\), approximate \(y(1.5)\) by completing the formulas in the code cell below.

Solution to Question 11:





Replace each ?? in the code below with an appropriate formula.




#import numpy as np

v = np.zeros(n+1)  # creates a vector of zeros where we'll store output
dt = 0.5  # define the step size

# Define diffeq
def diffeq(t, y):  # t is independent variable and y is dependent variable
    return y + t  # Use t and y for ind and dep variables

# Initial value
v[0] = 4  # initial condition is y_0 = 4

# Result after step 1
v[1] = ??

# Result after step 2
v[2] = ??

# Result after step 3
v[3] = ??

print("After step 1, we have y(0.5) is approximately", v[1],
     "\n \n After step 2, we have y(1) is approximately", v[2],
     "\n \n After step 3, we have y(1.5) is approximately", v[3],)

Computing Euler’s Method with the euler_method Function


The step-by-step method derived above is called Euler’s method. In the code cell below, we import a function called euler_method() from the ode_tools module that performs identical calculations as the code above. See ODE Tools Tutorial for a quick reference on how to use euler_method() and other functions for Chapter 1.

  1. Define diffeq.

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

  3. Define the step size dt and number of steps n.

  4. We import the euler_method() function.

  5. Calculate each step with the function euler_method(diffeq, t0, x0, dt, n).

from utils.ode_tools import euler_method

# Define diffeq
def diffeq(t, y):  # t is independent variable and y is dependent variable
    return y + t  # Use t and y for ind and dep variables

# Initial value
t0 = 0 # initial value of input
y0 = 4 # initial value output when t = t_0

# Step size and number of steps
dt = 0.5
n = 3

# Apply Euler's method
euler_method(diffeq, t0, y0, dt, n)

Question 12:


Using the same differential equation from Question 11, experiment with the code below to estimate \(y(1.5)\) accurate to one decimal place. How many steps did you use? What is the corresponding step size?

Solution to Question 12:





Replace each ?? to define appropriate values for n and dt in the code cell below.






# We are not changing the differential equation or initial conditions
# Thus we do not need to redefine diffeq, t0, or y0

# Number of steps and step size
n = ??
dt = ??

# Apply Euler's method
euler_method(diffeq, t0, y0, dt, n)

Question 13:


Consider the differential equation \(\dfrac{dy}{dt} = y+t\). Estimate the value of \(y(2)\) on the solution that passes through the point \((1, 3)\) using 10 steps. Use the euler_method() function in the ode_tools module to perform the calculations.

Solution to Question 13:





Replace each ?? to define appropriate values or expressions in the code cell below.






# Define diffeq
def diffeq(t, y):  # t is independent variable and y is dependent variable
    return ??  # Use t and y for ind and dep variables

# Initial value
t0 = ?? # initial value of input
y0 = ?? # initial value output when t = t_0

# Step size and number of steps
dt = ??
n = ??

# Apply Euler's method
euler_method(diffeq, t0, y0, 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.
Based on a work at https://github.com/CU-Denver-MathStats-OER/ODEs and original content created by Rasmussen, C., Keene, K. A., Dunmyre, J., & Fortune, N. (2018). Inquiry oriented differential equations: Course materials. Available at https://iode.sdsu.edu.