Day-18_Pre-Class_ModelingWithODEs-STUDENT - Jupyter Notebook
.pdf
keyboard_arrow_up
School
Michigan State University *
*We aren’t endorsed by this school
Course
201
Subject
Mathematics
Date
Apr 26, 2024
Type
Pages
16
Uploaded by BailiffFrogMaster671 on coursehero.com
3/17/24, 11
:
37 PM
Day-18_Pre-Class_ModelingWithODEs-STUDENT - Jupyter Notebook
Page 1 of 16
http://localhost:8888/notebooks/Downloads/Day-18_Pre-Class_ModelingWithODEs-STUDENT.ipynb
Day 18: Pre-class Assignment: Modeling with
Ordinary Di
ff
erential Equations
Put your name here
Goals for today's pre-class assignment
In this assignment, you will:
Create a model for a skydiver falling using di
ff
erential equations
Setup a system
of di
ff
erential equations (i.e., more than 1).
Solve these systems of di
ff
erential equations using the update method from the
previous assignment.
Solve these systems of di
ff
erential equations using solve_ivp
We will build o
ff
the previous assignment where we introduced using update equations to
numerically integrate a (mathematical) function. Here we will expand our tool kit by
numerically solving multiple
di
ff
erential equations simultaneously, and we'll solve them using
a tool built into SciPy called solve_ivp
.
But first, let's talk about the role of di
ff
erential equations in modeling.
3/17/24, 11
:
37 PM
Day-18_Pre-Class_ModelingWithODEs-STUDENT - Jupyter Notebook
Page 2 of 16
http://localhost:8888/notebooks/Downloads/Day-18_Pre-Class_ModelingWithODEs-STUDENT.ipynb
The Role of Di
ff
erential Equations in Computational
Modeling
We often use equations in computational modeling, and many of the systems we wish to
model contain things that are changing
:
changing in time (E.g., Temperature or heart rate)
changing in space (E.g., Temperature or elevation)
changing strategies (E.g., deciding whether to invest)
changing numbers (of people, molecules, stars, etc.)
changing prices
and so on...
In mathematics, we describe change with calculus and derivatives in particular. When an
equation has a derivative in it, it is called a "
di
ff
erential equation
(https://en.wikipedia.org/wiki/Di
ff
erential_equation)
" (as opposed to, say, an algebraic
equation). The types of equations can be used to model many di
ff
erent systems, and in this
assignment, we'll see how we can use them to model the free-fall of a skydiver.
Question
: What is a value or system that you deal with on a daily basis that is
changing?
A value that I deal with is money.
3/17/24, 11
:
37 PM
Day-18_Pre-Class_ModelingWithODEs-STUDENT - Jupyter Notebook
Page 3 of 16
http://localhost:8888/notebooks/Downloads/Day-18_Pre-Class_ModelingWithODEs-STUDENT.ipynb
Modeling a falling skydiver without air resistance
In this assignment, we will be modeling the motion of a skydiver. There are two variables that
will be changing that we need to consider:
The height
of the skydiver (I.e., how far above the ground they are). This value will
change with time as the skydiver gets closer and closer to the ground during their fall.
The speed
(or, really, the velocity
) of the skydiver * (I.e., how fast they are falling towards
the ground). The longer the skydiver falls, the faster they will be falling.
Since we have two values that are changing, we will need two di
ff
erential equations. We can
express these two values using update equations:
The main variables to know are: refers to height, and refers to velocity.
Note 1:
, meaning that velocity is defined as the derivative of height.
Note 2:
For the first part of this problem, , where is a constant equal to .
(Everything on Earth picks up speed at roughly the same rate, as long as we ignore air
resistance.
(https://www.youtube.com/watch?v=frZ9dN_ATew)
)
=
+
new
old
=
+
new
old
=
=
-
9.81
The derivs
Function
In order to solve these problems numerically, we need to specify what and are. We’re
going to use a derivs
function, just like we did in the previous assignment, except this
time it will return the derivatives for two
equations. Specifically, we want it to return:
Putting it into code, we get the following:
=
=
3/17/24, 11
:
37 PM
Day-18_Pre-Class_ModelingWithODEs-STUDENT - Jupyter Notebook
Page 4 of 16
http://localhost:8888/notebooks/Downloads/Day-18_Pre-Class_ModelingWithODEs-STUDENT.ipynb
In [1]:
The derivs
function is the heart of how we will solve di
ff
erential equations.
(It may seem silly to define a function that takes in two values and returns the same two
values, but we'll be making it more complex in a second.)
Initial Values
We first will begin by assuming:
meters
meters/second
gravity: meters/second^2
falling duration: seconds
timestep
= 3000
0
= 0
0
=
-
9.81
= 30
= 1
Running the Model
This code numerically solves for the motion of the skydiver and plots the result. It
should be similar to the code you wrote in your previous assignment. Review each line
of code an make sure you understand what it's doing and then execute the code.
In [2]:
# Create a function to compute derivatives of velocity and height
def
derivs
(v,g):
# derivative of height is velocity
dhdt =
v
# derivative of velocity is gravity for this example
dvdt =
g
return
dhdt, dvdt
# Import modules
import
matplotlib.pyplot as
plt
# Initialize variables
h =
3000 # initial height: m
v =
0 # initial velocoty: m/s g =
-
9.81 # gravity: m/s^2
t =
0 # initial time
tmax =
30 # Falling time
dt =
1 # timestep
3/17/24, 11
:
37 PM
Day-18_Pre-Class_ModelingWithODEs-STUDENT - Jupyter Notebook
Page 5 of 16
http://localhost:8888/notebooks/Downloads/Day-18_Pre-Class_ModelingWithODEs-STUDENT.ipynb
dt =
1 # timestep
# Initialize lists for storing data
height =
[]
velocity =
[]
time =
[]
# Append initial values to lists
height.append(h)
velocity.append(v)
time.append(t)
# Create a time loop that will update the skydiver over time
# Use a while loop that will loop until t > tmax
while
t <=
tmax:
# Compute derivatives for use in update equations
dhdt, dvdt =
derivs(v,g)
# Update Equations
h_new =
h +
dhdt
*
dt # new height
v_new =
v +
dvdt
*
dt # new velocity
# Append values to height and velocity lists
height.append(h_new)
velocity.append(v_new)
# Update old height/velocity with new height
h =
h_new
v =
v_new
# Increase time
t +=
dt # t = t + dt
# Update time list
time.append(t)
# Plotting height/velocity vs time plt.figure(
1
)
plt.plot(time, height, color =
'green'
)
plt.xlabel(
'Time [seconds]'
)
plt.ylabel(
'Height [meters]'
)
plt.grid()
plt.figure(
2
)
plt.plot(time, velocity, color =
'purple'
)
plt.xlabel(
'Time [seconds]'
)
plt.ylabel(
'Veloctiy [meters/second]'
)
plt.grid()
3/17/24, 11
:
37 PM
Day-18_Pre-Class_ModelingWithODEs-STUDENT - Jupyter Notebook
Page 6 of 16
http://localhost:8888/notebooks/Downloads/Day-18_Pre-Class_ModelingWithODEs-STUDENT.ipynb
-50
Veloctiv
[meters/second]
-100
-150
-200
-250
-300
10
15
Time
[seconds]
20
25
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help