LS 30A related items
iteration
solving differential equations (desolve_odeint)
plotting a timeseries
plotting a trajectory
plotting a vector field
Iteration
The word iterate loosely means "to do something repeatedly". Its mathematical definition is slightly more precise: to iterate means to repeatedly perform the same calculation or procedure, and to use the result of the previous calculation as the input to the next one.
The following is a quick example of that for a system that grows exponentially, doubling each time:
This worked well, but what if we wanted to view a time series of the result? Instead of just overwriting the variable each time, we can just append each entry to a list and then plot them afterwards.
if we plot the data, we can easily see that this is an exponential function.
Solving a Differential Equation
In LS30, we will encounter two types of differential equations: ordinary differential equations (ODEs) and delay differential equations. This section will concentrate on solving ODEs. ODEs have the mathematical form where is the rate of change of the states .
Generally, we will go through these steps, in about this order:
Define the differential equation, the initial conditions and the timespan of interest.
Solve the differential equation
Display the results as either a time series or a trajectory.
Let's illustrate this process using the Lotka-Volterra (Shark-Tuna) model. We'll use the convention that the data will be represented in the order (N,P)
.
The Lotka-Volterra model has the pair of differential equations
and we want to simulate a trajectory of this system with an initial condition of (50, 75) and with a timespan from (0,100) time units and a stepsize of 0.1.
There are a couple of common pitfalls in step 2:
switching the order of the variables in dvars
starting with invalid or inappropriate initial conditions for ic
using a timespan that either is too short to see the behavior we're interested in, has so many points that it takes too long to calculate, or has a timestep that is too large so the solution is not very accurate.
NB: Many other people prefer to write the differential equations straight into the differential equation, like this:
This is fine. It just means that if you need to change parameters or write something different, you may have to track down changes over a lot of cells.
Plotting a Timeseries
A time series is a series of data plotted on the vertical axis with corresponding times on the horizontal axis. Usually to do this, you'll need a list/vector of times, and a list (or multiple lists) of data and then you'll zip
them together and then plot them.
In the previous cell, list_plot(times, data) returns an error because list_plot only accepts lists of coordinates, not individual lists.
In situations where you just want to the view the elements in a list, you can just use the list_plot
function with the list as the only argument. This plots the elements of the list on the y axis and the index of each element on the x axis.
Plotting a Trajectory
A trajectory (sometimes referred to outside of this course as a phase portrait) is a plot of the state-data from a dynamical system. In contrast to a timeseries, trajectories do not explicitly show what time the data is from.
If the trajectory that you want to plot came from the desolve_odeint
command, then you're all set. Just use list_plot(sol)
and everything will come out alright. Since the output of desolve_odeint
is a numpy array with each of the columns corresponding to the time series of each state, sol
already has the "zipped list" characteristic built in.
If you print out the content of sol
itself, it is a little more clear what is going on:
Notice that up at the top, 1 and 2 are the initial conditions. Accordingly, they are at the beginning of the array columns. (Remember that each column is a timeseries for a single state)
Likewise, since each column of sol
is a time series, we can also zip
them together and achieve the same result as if we had just two separate lists zipped together.
Plotting a Vector Field
A differential equation has a function that maps each coordinate of the state space to a change vector. (or the tangent bundle for the mathematicians) We can visualize 2D vector fields easily using the command plot_vector_field
. The syntax is very similar to the plot
command.
Since plot_vector_field
returns a Graphics
object, (plot
, list_plot
are also Graphics
objects) we can overlay these together to get some nice results combining trajectories and the vector field. Notice how the trajectory seems to "follow" the path hinted at by the green change vectors.
Plotting tricks
limiting size of graph with xmin, xmax, ymin, ymax
creating empty figure with list_plot([])