This tutorial outlines how to use MAPLE to differentiate functions,
integrate functions analytically and numerically, and solve systems of
first-order ordinary differential equations. To work through it, you
should have a good working knowledge of MAPLE command syntax, arithmetic
operations, defining expressions and functions (and what the difference is
between them), and plotting. If you don't, you are advised to work through the
first tutorial in this series and then come back to this one.
OPTIONS
Click on your selection.
Go to the first MAPLE tutorial.
Ordinary differential equations.
Browse Richard Felder's home page.
If you're running on EOS, bring up the application menu (point to an open space on the desktop and click the middle mouse button) and choose Math and Statistics - Maple(4). (Alternatively, go into the directory where you wish to work and at the command prompt in the Xterm window, type add maple and then xmaple &)
A MAPLE window should come up, with an untitled worksheet superimposed over it.
Tip:
Once you've done something in a program step (like redefine a variable), it stays done unless you change it again. Even if you click on an earlier step and re-execute it, the effect of the change remains.
If a program does not seem to be working, before trying anything else re-execute all steps from the beginning by clicking on the Edit menu and selecting Execute - Worksheet. (You can also select a set of commands and re-execute them by selecting Execute - Selection.)
When completing this tutorial, type the boldface commands preceded by the MAPLE prompt [> (don't retype the prompt), and hit the "Return" or "Enter" key after typing the semicolons or colons that conclude each command. Following each command that ends with a semicolon, we will give the MAPLE output in italics. (If a command ends with a colon the output is suppressed.)
Differentiating an expression (as opposed to a function).
Once an expression for f(x) has been defined, use the diff(f,x) command to obtain an expression for the first derivative (df/dx), the diff(f,x,x) command to obtain the second derivative (d2f/dx2), etc.
In the set of commands that follows, we define an expression for a variable, f(x); review how to evaluate it for x=1, determine expressions for its first and second derivatives, and evaluate those expressions with four significant figures for the same value of x.
[> restart;
[> f := x^3 + 5*exp(2*x);
f := x3 + 5e2x
[> subs(x=1,f);
[> evalf(subs(x=1,f), 4);
[> df := diff(f,x);
[> df1 := evalf(subs(x=1,df),4);
[> d2f := diff(f,x,x);
[> evalf(subs(x=1,d2f),4);
Differentiating a function.
Even though an expression for f(x) and a function f(x) may look the same to you, they are treated in totally different ways by MAPLE. In this subsection, we define a function g(x), determine its first and second derivatives using the D(g) and D(D(g)) commands, and evaluate the derivatives at x=1. Remember that the diff command is used to differentiate expressions and the D command is used to differentiate functions.
[> restart;
[> g := x -> x^3 + 5*exp(2*x);
[> dg := D(g);
[> evalf(dg(1), 4);
[> d2g := D(D(g));
[> evalf(d2g(1),4);
Click here to return to list of options.
Indefinite integration of an expression
The command int(f,x) is used for both indefinite and definite integration of an expression (not a function) for f(x).
The next commands define an expression for f(x), integrate it (the expression for the integral does not include a constant of integration), evaluate the integral [F(x)] at a specified value of x, and check the integration by differentiating F to obtain f again. Note that MAPLE is case-specific, so that it considers f and F to be two different variables.
[> restart;
[> f := 3*x^2 10*exp(2*x);
[> F := int(f,x);
[> evalf(subs(x=1,F), 4);
[> diff(F, x);
We next convert the expression for F(x) into a function using the unapply command, evaluate the function at x=1, and differentiate F(x) with the D(f) command to obtain the original expression for f(x) as a function.
[> F := unapply(F,x);
[> evalf(F(1),4);
[> D(F);
Definite integration
The int command may also be used to calculate a definite integral. Instead of int(f,x), the command becomes int(f,x=a..b), where a and b are the lower and upper limits of the integral.
[> restart;
[>f := 3*x^2 10*exp(2*x);
[> A := int(f, x=0..1);
[> evalf(A,4);
Numerical integration
Some functions cannot be integrated analytically, but the definite integrals of such functions still have meaning (the area under a plot of f vs. x between the limits of integration) and MAPLE can determine them. For example, the function f(x) = exp( x3) has no analytical integral (convince yourself), but a plot of f vs. x> is a well-behaved curve that looks like this:
(The apparent gap near the f axis is not real--the curve goes smoothly to f=1 at x=0.)
Let us integrate this function from x=0 to x=2.
[> restart;
[>f := exp( x^3);
[> F := int(f, x=0..2);

MAPLE returned only the definite integral, indicating that it could not find an analytical expression. However, it has the desired value.
[> evalf(F,4);
Click here to return to list of options.
SOLVING ORDINARY DIFFERENTIAL EQUATIONS
The equations to be solved have the following general form:
where
t is the independent variable
y1(t), y2(t),..., yn(t) are the dependent variables (or state variables)
y1i is the initial condition for the first state variable (the value when t=to)
f1, f2,...,fn are known functions of t and the y's (state functions).
MAPLE can either solve the differential equations analytically (if analytical solutions exist) or numerically. The solutions are expressions for y1(t), y2(t),...,yn(t) (if the equations are solved analytically) or plots or tables of y1, y2,..., yn at values of t from t= to (which is usually 0) to some final value.
A pair of chemical reactions, A->B and B->C, take place in a batch reactor, starting with pure A at a concentration CA0 = 1.00 mol/liter. The following equations describe how the concentrations CA(t), ,CB(t), and CC(t) vary with time(sec). The notation CA' will be used to represent the derivative dCA/dt.
CA' = -0.1CA , CA(0) = 1
CB' = 0.1CA - 0.2CB , CB(0) = 0
CC' = 0.2CB , CC(0) = 0
These equations have the form of the set that began this document. (The independent variable, t, does not appear explicitly in the state functions on the right-hand sides, but nothing says it has to.)
Initially the reactor contains pure A at a concentration of 1.00 mol/L. As time increases, A is steadily consumed in the first reaction . B forms in the first reaction and its concentration initially increases, but it is also steadily consumed in the second reaction and so its concentration eventually starts to decrease and approaches zero at large times. C forms in the second reaction, slowly at first since it cannot appear until some B has been formed, and eventually it is all that is left in the reactor. You would expect plots of the three concentrations vs. time to appear as follows:

The curves of Ca vs. t and Cb vs. t are not identical at large times but they are too close together to be distinguished on the scale of this plot.
The goal is to write a MAPLE program to solve the equations and to
calculate and plot Ca, Cb, and
Cc at times from t=0 to t=40 s. First solve
the equations analytically, then numerically.
Add MAPLE's plotting package
[> with(plots): (return key)
This command makes a number of plotting routines available to you. (You
could see what they are by using a semicolon instead of a colon at the end of
the command.) We will use the plot, textplot, and display
routines in the package at different places in the tutorial.
Enter the differential
equations to be solved
When entering the next command, don't worry about the error messages that
appear after you hit the return key without typing a semicolon or colon at
the end of the first three lines; just keep typing. MAPLE will treat the
second, third, and fourth lines as continuations of the statement.
[> deqs := (return key)
> diff(Ca(t),t) = -(1/10)*Ca(t) , (return key)
> diff(Cb(t),t) = (1/10)*Ca(t) - (2/10)*Cb(t), (return key)
> diff(Cc(t),t) = (2/10)*Cb(t) ; (return key)
You could have entered the MAPLE text on one line, but it is easier to read and modify the statement if you enter each equation on a separate line.
[> inits := Ca(0)=1 , Cb(0) = 0 , Cc(0) = 0;
[> fcns := [Ca(t), Cb(t), Cc(t)];
If you wished to solve the equations numerically, you would skip the next few sections. You may do so now or continue with the analytical solutions.
Go to numerical solution of ODE's.
[> dsolve({deqs, inits}, fcns);

Here are some additional notes about the dsolve command.
Even though dsolve seems to have solved the equations and printed the solutions, MAPLE does not "know" what the expressions for Ca(t), Cb(t), and Cc(t) are. To do anything with the solutions, like plotting them, you need to first define them as functions, which is what we do next.
For each of the following commands, you can copy and paste from the dsolve output instead of retyping the expressions. (Use the "Copy" command in the Edit menu, not "Copy as Maple text.")
[> Ca := t -> exp( -1/10*t) ;
[> Cb := t -> exp( -1/10*t) - exp( -1/5*t) ;

[> Cc := t -> 1 - 2*exp( -1/10*t) + exp( -1/5*t);
The command that follows is a single command, which we break into different lines for ease of viewing. The parenthetical explanations not in boldface should not be typed.
[> P := plot( [Ca(t), Cb(t), Cc(t)], t=0..40,
> title = `CONCENTRATION (MOL/L) VS. TIME(SEC)`, (use forward quotes)
> titlefont = [HELVETICA, BOLD, 14], (font, font style, font size in pts)
> labels = [`t`, `C`], (axis labels - 6 characters max.)
> style=[point, point, point], (point as opposed to line)
> symbol=[circle, box, diamond],
> color=[red,green,blue] ):
[> display ([P]);
A plot like the one shown at the beginning of this tutorial should appear.
Next prepare labels for the three curves on the chart. With the cursor next to the prompt (>) on the line just created, type on a single line
followed by the "Return" key.
The variable "labela" consists of a label (Ca) and the coordinates (abscissa = 0.8, ordinate = 0.5) of the desired location of the label on the plot to be generated by the subsequent display command. labelb and labelc have similar meanings. It generally takes a few tries to get the text exactly where you want it.
Finally, modify the "display" command to insert the labels. Make the command read
[> display([P, labela, labelb, labelc]);
Then hit the "Return" key to generate the modified chart.
We could at this point generate a table of values of Ca, Cb, and Cc at specified times, but we will defer that until we have generated numerical solutions.
The linear first-order differential equations in this example have an analytical solution, which MAPLE found. Often analytical solutions to differential equations do not exist and numerical equations must be obtained. In this section we show how to do so.
First, turn Ca, Cb, and Cc from functions back into variable names.
[> Ca := 'Ca': Cb := 'Cb': Cc := 'Cc':
If you had not generated analytical solutions first but simply wanted to get numerical solutions, you would have jumped right from the fcns := ... statement to the statements in the next section.
To solve differential equations numerically, you must
first add MAPLE's differential equations package.
[> with(DEtools):
If you typed a semicolon instead of a colon, you would get a list of the
different routines in this package. The only one we will use is DEplot.
Next, create and store separate plots of the dependent variables vs. the
independent variable (but don't display them yet).
[> Caplot := DEplot ( [deqs], fcns, t=0..40, [[inits]],
scene=[t,Ca], linecolor=red, stepsize=0.2, arrows=none):
[> Cbplot := DEplot ( [deqs], fcns, t=0..40, [[inits]],
scene=[t,Cb], linecolor=green, stepsize=0.2, arrows=none):
[> Ccplot := DEplot ( [deqs], fcns, t=0..40, [[inits]],
scene=[t,Cc], linecolor=blue, stepsize=0.2, arrows=none):
If you get an error message after any of these commands, go back and
re-execute the deqs := , inits := , and
fcns := commands and try again.
The first of the DEplot commands leads to the numerical solution of the
equations in deqs with initial conditions in inits. The solution
will be values of the variables in fcns (Ca, Cb, and Cc) at intervals of
0.2 time units (stepsize) from t=0 to t=40. After the
command is executed, Caplot will contain the coordinates of a plot of Ca
vs. t (the variables specified in the scene command) that will appear as
a solid red curve when the display command is used to show the graph. The
"arrows=none" specification suppresses the generation of a direction field. (If
you don't know what that means, don't worry about it.)
The DEplot command uses a fourth-order Runge-Kutta algorithm (other options
exist if you specify them with additional arguments) to generate plots of
solutions of differential equations. If you ended these three statements with
semicolons, MAPLE would have displayed the coordinates of the plots at 0.2
second intervals along with other information about the plot, but not the plot
itself. If you just entered the DEplot commands (rather than Caplot :=
DEplot...) followed by semicolons, the three plots would have been
displayed on separate graphs. By adding options to the DEplot command you can do a number of additional
things, like specifying display ranges for the dependent variables, stopping
the integration if the dependent variables go outside the specified ranges,
changing solution algorithms, generating direction (slope)
fields, and integrating higher-order differential equations. To see the
options, click on "DEplot" in any of the commands where it appears and then
select the HELP menu, or type [> ?DEplot . (Don't do it now.)
Next, if you have not already done so in this tutorial, prepare
labels for the three curves on the chart. Click below if you previously created
labela, labelb, and labelc.
With the cursor next to the prompt
(>) on the line just created, type on a single line
followed by the "Return" key.
The variable "labela" consists of a label (Ca) and the
coordinates (abscissa = 0.8, ordinate = 0.5) of the desired
location of the label on the plot to be generated by the subsequent display
command. labelb and labelc have similar meanings.
It generally takes a few tries to get the text exactly where you want it.
We are finally ready to display the three generated plots on a single chart.
[> display ( [Caplot, Cbplot, Ccplot, labela, labelb,
labelc] );
The desired chart should appear. If the plots appeared as a set of
segments with discontinuous slopes instead of smooth curves, we would
re-execute the DEplot commands with a smaller step size.
[> soln := dsolve({deqs, inits}, fcns, numeric);
At this point the three equations have been solved using the 4th-order
Runge-Kutta algorithm to generate a numerical function soln(t)
containing all three concentrations as functions of time.
Display the concentrations at a specified time (t=5), each with
four significant figures.
[> evalf(soln(5), 4);
[ t = 5., Ca(t) = .6065, Cb(t) = .2387,
Cc(t) = .1548) ]
Display a table of concentrations at times 0, 2, 4,..., 20 with four
significant figures. (The next command illustrates MAPLE's powerful programming
capability, which we have not discussed in these tutorials.)
[> for k from 0 to 10 do evalf(soln(2*k), 4); od;
[ t = 0, Ca(t) = 1, Cb(t) = 0, Cc(t) = 0]
[ t = 2, Ca(t) =.8187, Cb(t) =0.1484,
Cc(t) = .03286 ]
[ t = 4, Ca(t) =.6703, Cb(t) =0.2210,
Cc(t) = .1087]
[ t = 20, Ca(t) =.1353, Cb(t) =0.1170,
Cc(t) = .7476]
This repetition statement (or for loop) instructs MAPLE to
execute the evalf command repeatedly, substituting k = 0, 1, 2,..., 10
wherever k appears. When k=0, 2*k=0 and MAPLE executes the
command evalf(soln(0),4) to evaluate the three concentrations at t=0;
when k=1, 2*k=2 and MAPLE executes evalf(soln(2),4), etc. If
several commands are placed between "do" and "od;", MAPLE will execute them in
sequence each time it goes through the loop.
The tabulated results indicate that Cb goes through a maximum
somewhere between 6 and 8 seconds (verify this statement). To get a better
estimate of the maximum you could evaluate soln at closely spaced times
between t=6 and t=8. (Don't do it now.)
Finally, clean up the worksheet by deleting the next-to-last paragraph.
Click on the bracket ( [ ) to the left of the evalf(soln(5),4); command.
Choose Delete Paragraph from the Edit menu. The command and the
MAPLE response should be gone.
Generate numerical solutions of the equations
and plot them
Label the curves on the plot
labela := textplot( [.8, .5, `Ca`] ):
labelb := textplot( [4, .26, `Cb`]):
labelc := textplot( [20, .8, `Cc`] ):
Display the graph
Generate and store numerical solutions of the three equations
soln := proc(rkf45_x) ... end
Click here to return to list of options.
Insert text in the worksheet to prepare a report (if one is required as an
assignment)
MAPLE TUTORIAL II. CALCULUS AND DIFFERENTIAL EQUATIONS
Don't hit "Return" yet.
Print your worksheet
Click here to return to list of options.
Exit from MAPLE.
Comments or questions? You can send mail to Dr. Felder at felder@eos.ncsu.edu