Our text, Differential Equations: A Modeling Perspective by Borrelli and Coleman, requires the use of a "numerical solver" (technology to approximate, using numbers, solutions to differential equations) from the beginning. Eventually (chapters 6 and 7 of the lab-book, Differential Equations with Maple by Coombes et al), we'll learn all about Maple's ability to numerically estimate and graph approximate solutions to differential equations. At this point however, Maple is most likely unfamiliar to us and spending enough time to get comfortable enough to fully understand the relevant Maple commands and their syntax would take too long. This brief introduction is designed to give you just enough "template" examples of using Maple's numerical solver, so you can complete the homework exercises in the first few sections of the text, using "copy and paste" techniques along with standard editing features.
For starters, we must load several relevant Maple packages for differential equations and plotting. To use any of the template examples in this file, you must first (once only, each time you start a new Maple session) execute the command below. To do this, place the cursor anywhere in the red command line and press ENTER. If you're reading this via the Web, copy the red text below into a Maple worksheet and then execute the command as described in the last sentence above.
> map(with, {plots,plottools, DEtools}):
Warning, new definition for translateMost problems requiring a numerical solver can be done with Maple's DEplot command. This command is very robust and has many options. We start by using many of the default values, specifying very little "extra information". At a minimum, Maple needs to know the differential equation (or equations) to solve, what the dependent variable is (the function you are trying to find, which is described by the differential equation), the initial conditions (usually a point (t0, y0) which you would like to be on the graph of your solution), and the range (of the independent variable first and the dependent variable second) over which to plot the approximate solution. By default, Maple displays "arrows" which outline "all solutions". We'll see how to turn this option off in a bit and better understand how to "read" these arrows in a few days. Section 1.2 calls these arrows a "direction field". Figure 1.1.3, on page 6 of the text, displays several numerical solutions to y' = y - y^2 / 12 (with different starting values y(0) = y0). I'll use this plot as a template and give several possible ways to modify this example. The bare minimum gives the differential equation (using Maple's syntax for derivatives), the dependent variable (y(t) in this case), and the range for all variables, the independent variable (t here) is first, then the dependent variable (y in this example).
> DEplot(diff(y(t),t) = y(t) - y(t)^2/12, y(t), t=0..10, y=0..20);
![[Maple Plot]](images/desolverintro2.gif)
You can "see" several solutions, by following the arrows. To plot specific solutions, you specify initial conditions. The initial conditions go inside square brackets, with each initial condition inside its own set of square brackets, and initial conditions should follow the independent variable range. Here is an example which plots one solution that satisfies y(0) = 2 (press "SHIFT ENTER" for a line break in a Maple command):
> DEplot(diff(y(t),t) = y(t) - y(t)^2/12,
y(t),
t=0..10, [[y(0)=2]], y=0..20);
![[Maple Plot]](images/desolverintro3.gif)
Two initial conditions:
> DEplot(diff(y(t),t) = y(t) - y(t)^2/12,
y(t),
t=0..10, [[y(0)=2],[y(0)=16]], y=0..20);
![[Maple Plot]](images/desolverintro4.gif)
Make the solutions specific colors:
> DEplot(diff(y(t),t) = y(t) - y(t)^2/12,
y(t),
t=0..10, [[y(0)=2],[y(0)=16]],y=0..20, linecolor=[blue,black]);
![[Maple Plot]](images/desolverintro5.gif)
Removing the arrows, making all solutions the same color, with 3 solutions:
> DEplot(diff(y(t),t) = y(t) - y(t)^2/12,
y(t),
t=0..10, [[y(0)=2],[y(0)=4],[y(0)=16]], y=0..20,
linecolor=blue, arrows=NONE);
![[Maple Plot]](images/desolverintro6.gif)
Essentially the plot in figure 1.1.3, where we first define all of the initial conditions (the long way). Be sure to execute this command (by placing the cursor anywhere in the red command line and pressing ENTER), since the definition of inits will be re-used repeatedly below.
> inits:=[[y(0)=.1],[y(0)=.5],[y(0)=1],
[y(0)=2],[y(0)=4],[y(0)=6],
[y(0)=8],[y(0)=10],[y(0)=12],[y(0)=14],
[y(0)=16],[y(0)=18],[y(0)=20]]:
DEplot(diff(y(t),t) = y(t) - y(t)^2/12, y(t),
t=0..10, inits, y=0..20,
linecolor=black, arrows=NONE,title=`Figure
1.1.3`);
![[Maple Plot]](images/desolverintro7.gif)
Somewhere around t = 5, many of the solutions plotted above seem to run together. You can "zoom in" by specifying an x (or t) and y range, over which to plot the solutions, with the view option as follows. The plot below suggests that these solutions do not actually intersect, they just get close together.
> DEplot(diff(y(t),t) = y(t) - y(t)^2/12,
y(t),
t=0..10, inits, y=0..20,
linecolor=black, arrows=NONE, view=[4..6,
11..12.1]);
![[Maple Plot]](images/desolverintro8.gif)
You can see the linear pieces of the numeric solutions above, especially the ones in the southeast corner of the plot. You can make the linear pieces shorter (or longer) using the stepsize option. The plot above seems to have a stepsize, or horizontal "length of linear pieces" of about 0.5. Below, the same plot is shown with a stepsize of 0.1. Smaller stepsizes give more accurate results which look smoother, but require significantly more computational time and resources.
> DEplot(diff(y(t),t) = y(t) - y(t)^2/12,
y(t),
t=0..10, inits,y=0..20, linecolor=black,
arrows=NONE, stepsize=0.1, view=[4..6, 11..12.1]);
![[Maple Plot]](images/desolverintro9.gif)
Here is a "piecewise defined" function in Maple. The arrow is formed by a minus sign, and a bigger than sign with no spaces between them. The syntax is roughly function_name := var -> piecewise( conditon1, formula1, condition2, formula2, ...);
Maple will test the conditions left to right and apply the first formula that has a true condition. This allows some savings in typing, as you need not build into condition 2, the fact that condition 1 is false (since Maple will never reach condition 2 unless condition 1 was false). To define H(t) = 4 for t from 0 to 5, H(t) = 0 for t from 5 to 10, and H(t) = 1 for t from 10 to 15, you can drop the t > 5 from condition 2, and the t > 10 from condition 3, so it can be given as
> H := t -> piecewise(t<=5, 4, t<=10, 0, t<=15, 1);
If Maple uses condition 2 ( t is less than or equal to 10), it must be that condition 1 is false, so t > 5 automatically. Here is a plot of this function where I've told Maple to look for discontinuities (jumps), draw the graph in red and make it 3 times thicker than normal.
> plot(H(t),t=0..15,discont=true,color=red,thickness=3);
![[Maple Plot]](images/desolverintro11.gif)
Note that our H(t) above agrees with the one in the text on page 7, for t <= 10, so the command below produces something similar to figure 1.1.6 on page 7 of the text.
> DEplot(diff(y(t),t) = y(t) - y(t)^2/12-H(t),
y(t),
t=0..10, inits, y=0..20,
linecolor=black,arrows=NONE, title=`Figure
1.1.6`);
![[Maple Plot]](images/desolverintro12.gif)
The text describes a "square wave" function sqw(t, d, p) (in appendix B). The input variable (time) is t. d and p are parameters which describe the duty (on time percentage) and period (amount of time, t, before the graph repeats). You can view sqw(t,d,p) as a function which is ON (equal to one) for the first d percent of a period, and OFF (equal to zero) for the rest of a period. The periods start at integer multiples of p, and last for p time units (0 to p, p to 2p, 2p to 3p etc.). For a fishing season which is on for the first 2 months of each year, then off for the last 10 months, we can take p = 1 year, and d = 2/12 expressed as a percent, which is 100*2 / 12 = 50/3 percent. Here is a definition of the general sqw function, which you can copy and paste and reuse in the future. You should execute this command (once) before attempting to re-use it.
> sqw := (t,d,p) -> piecewise(p*(t/p-floor(t/p)) <= d*p/100, 1, 0);
Here, I have used the fact that piecewise accepts a "default clause" as the last argument, so if all earlier conditions are false, the default formula (0 in the case above) will be used. The complicated condition 1 above, asks Maple to determine if the input t is in the first d percent of a period. If it is, the value is one, otherwise the value is zero. Here is a plot of the harvesting function from problem 7 of section 1.1. It should equal 4 for the first 2/12's of a year, and 0 for the last 10/12's of a year. The vertical lines are NOT part of the true graph, just a clitch in Maple.
> plot(4*sqw(t,50/3,1), t=0..5,thickness=3);
![[Maple Plot]](images/desolverintro14.gif)
In general, sqw(t,d,p) will be on (equal to one) for the first d percent of a period, and off for the rest of the period. Here are a few examples to help you get the feel of this square on-off function.
Example 1: Periods last 8 time units and this function is on for the first 25% of each period.
> plot(sqw(t,25,8),t=0..24);
![[Maple Plot]](images/desolverintro15.gif)
Example 2: On for the first 33% of each period of length 6:
> plot(sqw(t,33,6),t=0..18);
![[Maple Plot]](images/desolverintro16.gif)
Example 3: On for the first 90% of a 10 unit period.
> plot(sqw(t,90,10),t=-20..20);
![[Maple Plot]](images/desolverintro17.gif)
Here is a plot similar to figure 1.1.8 on page 9 of the text. It has an eight month harvest of 4 tons at the start of each year, and requires a new set of initial conditions (which I just guessed at and typed in).
> DEplot(diff(y(t),t) = y(t) - y(t)^2/12
-4*sqw(t,100*8/12,1), y(t),
t=0..10, [[y(0)=3.8],[y(0)=3.9],[y(0)=4],[y(0)=4.1],
[y(0)=4.2],[y(0)=5],[y(0)=6],[y(0)=7],[y(0)=8],
[y(0)=10],[y(0)=12],[y(0)=16],[y(0)=20]],
y=0..20, linecolor=black, stepsize=.05,arrows=NONE,
title=`Long Harvest Season`);
![[Maple Plot]](images/desolverintro18.gif)
Sometimes, you'll want to add non-solution graphs to a DEplot. This can be done with the display command (in the Maple plotting packages loaded at the start of this file). It can also be done by printing a Maple worksheet and drawing the extra curves by hand. One way to use the display command, is to produce named (but not printed) versions of several different plots, and then combine them using display. Note that most of the Maple commands above end with a semicolon. This tells Maple to show the output of the command. If instead you end a command with a colon, Maple will carry out the command, but not produce the output. This is handy, since whenever you name a plot, Maple would display it as the internal Maple code to produce the plot (not the pretty picture you usually see). Look at the last plot above. It appears as if many solutions tend towards some eventual tonage of fish near 8 tons. If we wanted to add a thick red line at y(t) = 8 to that plot, here is what we want to add:
> plot(8,t=0..10,y=0..20,color=red,thickness=3);
![[Maple Plot]](images/desolverintro19.gif)
Since that plot is simple, the ugly Maple code to produce it is not so bad. Here is what happens if we try to name that plot, and ask Maple to display the result:
> lineplot:=plot(8,t=0..10,y=0..20,color=red,thickness=3);
Many Maple plots produce so much garbage output that it would take a long time just to scroll back to the command line that created the output, so using a colon to end the command is handy. Here I repeat (and name) the last long season harvest plot above, saving the title command to appear in the display command later, and ending this version with a colon.
> longplot:=
DEplot(diff(y(t),t) = y(t) - y(t)^2/12 -4*sqw(t,100*8/12,1),
y(t),
t=0..10, [[y(0)=3.8],[y(0)=3.9],[y(0)=4],[y(0)=4.1],
[y(0)=4.2],[y(0)=5],[y(0)=6],[y(0)=7],[y(0)=8],
[y(0)=10],[y(0)=12],[y(0)=16],[y(0)=20]],
y=0..20, linecolor=black, stepsize=.05,arrows=NONE,
title=`Long Harvest Season`):
Now that both the line and the DEplot are named and saved, I can display them together as follows:
> display([lineplot,longplot],title=`Long Harvest Season`);
![[Maple Plot]](images/desolverintro34.gif)
Shazam! To combine a bunch of plots, name each one, ending the commands with a colon, then put them inside square brackets as the first argument to a display command which ends with a semi colon! Actually, its a good idea to look at the plots you're about to combine (don't name them until they look good, once they look correct, name them and change the command ending semi colon to a colon) before you display them together.
Return to Differential Equations Materials Page.