All published worksheets from http://sagenb.org
Image: ubuntu2004
Numerically Solving Systems of Differential Equations
Later in the semester, we will take a closer look at numerical solvers for systems of differential equations. As it will turn out, Euler's method does work for systems, but not very well for many systems with a periodic solution (like the model of an ideal pendulum). So, for now, we will treat numerical solvers as a "black box": inputs go in, completely undetectable stuff goes on, and then outputs come out. The following notebook pulls together some of the SAGE package's tools for solving and plotting numerical solutions for 2-dimensional systems.
So, let's suppose that you want to solveYou can type:
solution=SolveSystem([f(t,x,y),g(t,x,y)],[t,t0,t1],[x0,y0])
and SAGE will produce a numerical solution, stored in the variable solution. For example, to solve the systementer
At this point, SAGE has stored a numerical solution as a list of triples . We will probably never want to look at the coordinates. Instead, we would like to see some plots of the solution. You can produce two plots easily: the phase plane portrait (the graph of the solution as a parametric curve in the plane), and the coordinate plots.
We would frequently like to see the "flow" of the differential equation. One way to do this is to randomly choose a bunch of initial points in a specified rectangle, solve the system of differential equations starting at each of the initial conditions, and then plot all of the solutions you just computed. The syntax for this is similar to the SolveSystem command:
solution=SolveFlow([f(t,x,y),g(t,x,y)],[t,t0,t1],[[a,b],[c,d]])
where describes the rectangle from which we will draw the initial conditions. As an example, let's plot the flow for the system we just solved.
This will very likely take a little longer than the first example did. By default, we are choosing 20 initial conditions and solving the differential equation for each. So, we should expect the approximation process to take about twenty times as long. (The number of randomly chosen initial conditions can be changed; see the section on options, below. For more initial conditions, more time is required.)
Now we plot the solutions in the phase plane and in coordinate space as we did before.
Setting Options for the Solver
At this time, you can tweak only a few options in the solver. Typing
solution=SolveSystem([f(t,x,y),g(t,x,y)],[t,t0,t1],[x0,y0],stepsize=decimal number)
will change the numerical step size the solver uses. (This is the same as the step size in Euler's Method.) By default, the stepsize is 0.05. Decreasing it will make solutions more accurate, but will require more computing time to solve.
For computing flows, two options may be set:
solution=SolveFlow([f(t,x,y),g(t,x,y)],[t,t0,t1],[[a,b],[c,d]],stepsize=decimal number,flows=positive integer)
The stepsize option is as above. The flows option determines the number of initial conditions to randomly choose in the specified rectangle . Increasing the number of flows to draw can result in a more complete picture, but requires more computing time. Setting this number above 100 is probably not helpful, since it will require a large amount of computing time and will clutter up the image with too much noise. By default, flows is set to 20.
Warnings and Notes
First, always use , , and as your variables when defining your differential equation. This restriction was made to simplify the command syntax.
Second, you can save the plots you make for printing or inclusion in homework writeups. Just right-click on the plot and select "Save image as..." or the corresponding choice in your web browser. (That is, treat the picture just like any picture in any web page.)
Third, the code at the bottom of this page is necessary for solving differential equations with this particular syntax. Don't delete it!
Code:
Modeling with Systems (Section 2.1)
In this section we look at two models: a predator-prey model and an oscillating spring system. The predator-prey model is a classic example of constructing more complex behaviors from simple models.
The Predator-Prey Example:
Begin with the two exponential growth models for the prey () and predator ():We then add an interaction term to "couple" the populations together.Let's take a look at the phase plane and coordinate plots.
We then modify the system to model logistic growth in the prey.
The Mass-Spring Example:
Let denote position and denote velocity. Then, we rewrite the second-order equationas the first-order system
Vector fields and solutions
Plotting a vector field in SAGE is straightforward. For example, we can plot a vector field for the system$\frac{dY}{dt}=\left[\begin{array}{c}2x-1.2xy\\ -y+0.9xy\end{array}\right]\ \ \ \ Y(0)=\left[\begin{array}{c}1\\ 0.5\end{array}\right]
We can add a solution curve (or more!) on top of this.