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 solve
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 system
|
|
At this point, SAGE has stored a numerical solution as a list of triples (t,x,y). 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 (x(t),y(t)) as a parametric curve in the (x,y) plane), and the coordinate plots.
|
|
The x coordinate is shown in blue, the y coordinate in red. The x coordinate is shown in blue, the y coordinate in red. |
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 [[a,b],[c,d]] 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.
|
|
The x coordinate is shown in blue, the y coordinate in red. The x coordinate is shown in blue, the y coordinate in red. |
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 [[a,b],[c,d]]. 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.
First, always use x, y, and t 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!
|
|
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.
Begin with the two exponential growth models for the prey (x) and predator (y):
|
|
|
|
The x coordinate is shown in blue, the y coordinate in red. The x coordinate is shown in blue, the y coordinate in red. |
We then modify the system to model logistic growth in the prey.
|
|
|
|
The x coordinate is shown in blue, the y coordinate in red. The x coordinate is shown in blue, the y coordinate in red. |
Let x denote position and y denote velocity. Then, we rewrite the second-order equation
|
|
|
|
The x coordinate is shown in blue, the y coordinate in red. The x coordinate is shown in blue, the y coordinate in red. |
|
|
|
|
|
|
|
|
Plotting a vector field in SAGE is straightforward. For example, we can plot a vector field for the system
|
|
We can add a solution curve (or more!) on top of this.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|