1
MAPLE TUTORIAL for Ordinary Differential Equations (ODEs)
Ver. 1.0
Raymon Baek
Vladimir Dobrushkin
Maple Tutorial Table of Contents
Preface………………………………………………………………………………………………………….…………..…3
1. Functions: Definition, as a Solution of ODE, Piecewise, Plotting…………………………….…4
- How to define functions……………………………………………………………………………....4
- Plotting functions………………………………………………………………………………………...5
- Plotting with axes, without axes…………………………………………………………………..6
- Implitictplot command…………………………………………………………………………………7
- Displaying multiple plots in the same/separate plane…………………………………..8
2. Solving ODEs: dfieldplot, DEplot, odeplot, Defining Function as ODE, Nullclines……...11
- Solving Using Direction/Slope Fields (dfieldplot)……………………………………….…12
- Solving Using DEplot……………………………………………………………………………….……13
- Solving Using odeplot…………………………………………………………………………….…….13
- Plotting Vertical Lines………………………………………………………………………………….14
3.Analytical Solutions (General and Particular Solutions) and Cauchy Problem…………..16
- Solving ODEs (General Solution)…………………………………………………………………..16
- Solving ODEs (Particular Solution)………………………………………………………………..16
- Initial Value Problem (Cauchy Problems)……………………………………………………..16
4. Sequences and Recurrences…………………………………………………………………………………....18
- Explicitly Defined Sequences………………………………………………………………………..18
- Recursively Defined Sequences…………………………………………………………………….18
- Using rsolve………………………………………………………………………………………………….18
- Resolving First Order Recurrences………………………………………………………………..19
5. Numerical Solutions: Plotting Difference Between Exact and Approximate in Regular Plot/Log Plot………………………………………………………………………………………………………………..20
- Euler’s Method…………………………………………………………………………………………….20
- Backwards Euler’s Method…………………………………………………………………………..22
- Trapezoid Rule……………………………………………………………………………………………..22
- Improved Euler’s Method…………………………………………………………………………….22
- Modified Euler’s Method……………………………………………………………………………..23
- First Order Polynomial Approximation…………………………………………………………24
- Second Order Polynomial Approximation…………………………………………………….24
- Third Order Polynomial Approximation………………………………………………………..25
- Regular Plotting vs. Log-Plotting…………………………………………………………………..26
- Runge-Kutta Method……………………………………………………………………………………26
6. Laplace Transform…………………………………………………………………………………………………….28
Preface
This tutorial is meant as an introduction to the mathematical program Maple created by MapleSoft© for the APMA 0330 course. This is a tutorial made solely for the purpose of education.
If you have not taken or are not taking a course regarding Maple or programming, such as CSCI 0150 or ENGN 0030, then please begin from Chapter 1. Otherwise, please begin from Chapter 2: Functions: Definition, as a Solution of ODE, Piecewise, Plotting. For those who have used Maple before, please note that there are certain commands and sequences of input that is specific to solving differential equations, so it is best to read through this tutorial in its entirety. This tutorial is based on Maple versions 10~15. Therefore, tutorials from other sources may or may not be compatible with this tutorial.
This tutorial corresponds to the Maple “mw” files that are posted on the APMA 0330 website. You, as the user, are free to use the m files to your needs for learning how to use the Maple program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.
1. Functions:
How to Define Function and Plots
How to define functions
In Maple, defining functions, or anything for that matter, is done by the command “:=”
Try the following input (Type in exactly as you see it. Do not include the right carrot , as this is only for showing Maple input in forms of text other than in Maple):
> |(x^3+x+sqrt(x))/(Pi*x-1)|;
The resulting output should be:
There are a few things to note when defining functions:
- Types of operations are expressed as + (addition), - (subtraction), *(multiplication), / (division).
- Exponents are expressed with carrots ^. Beware that when you input exponents, you must hit the right arrow key afterwards or else you will continue to type into the exponent. The same concept applies to division; when you are typing in the numerator/denominator, in order to move the blinking cursor outside of the fraction, you must hit the arrow keys to move it outside of the fraction (Clicking works as well).
- Absolute values are expressed with vertical lines ||. They can also be expressed using the abs( ) command.
- Square roots (radicals) are expressed with the input sqrt( ).
- When inputting π, you must type in Pi. For some reason, sometimes Maple will not accept pi as π.
- When using division, be sure to separate the numerators and denominators with parentheses to prevent errors.
- When you wish to substitute a variable with a value, use the subs command. The generic syntax is as follows:
> subs(value, equation);
Therefore, if we use the above equation to substitute x with the value -2, we have:
> subs(x=-2, %);
The % is a special symbol which takes in the most recently inputted equation. This is a useful symbol to use to avoid assigning equations to variables.
- You will notice in #7 that the resulting output will not be a specific value, but another equation with the x substituted with -2. Therefore, you may use the evalf command to receive a specific answer. The syntax is straightforward and simple, you just have to input the equation back in.
> evalf(%);
1.386687900
Plotting functions
Plotting in Maple is simple, but there is much to plotting than one may think.
Let’s begin by looking at the basic syntax of the plot command in Maple:
>plot(function, range, options(if any));
As you can see, the plot function involve a function, the x and y ranges of the plot, and any conditions if there are any present. The x and y ranges do not necessarily have to be inputted, but if there are other functions to plot (mentioned later), it is best to make the plot fit all functions by inputting the appropriate ranges. If you do not input a range for one function, then Maple will automatically generate a plot that fits the function. There are many options to choose from when using all types of plot commands. The most important options that can be used for ODEs are listed below, but others are listed in the Maple website:
Option / Code Input / NotesSpecify type of axes / axes=f / The value f can be boxed, framed, none, or normal. This option is automatically set to normal if not specified.
Coordinate system used for displaying axes / axiscoordinates=t / The value t can be either polar or Cartesian. This option is automatically set to Cartesian if not specified.
Specifying color for curves to be plotted / color=n
OR
colour=n / The value n can be replaced with any color. For simplicities sake, it’s best to use simple colors such as blue and red. This option is automatically set to the default color of the plot command (which is usually red).
Detecting discontinuities / discont=t / If a discontinuity exists in the specified function for plotting, then the t value is true. This option is automatically set to false if not specified.
Labeling axes / labels=[x,y] / This option specifies what the axes in the plot is labeled with. By default they have no labels.
Legend entry / legend=s / This option can be used for displaying a legend entry for each curve in the plot. The s value can be whatever the user desires. This option is automatically nonexistent if not specified.
Scaling of the graph / scaling=s / The s value can be constrained or unconstrained. The default value is unconstrained, which means the plot is scaled to fit the window. The constrained value makes all axes use the same scale so, for example, a circle would appear perfectly round.
Symbols used to plot graph / symbol=s / The s value can be asterisk, box, circle, diagonalcross, diamond, point, solidcircle, soliddiamond, or solidbox. This option is automatically nonexistent if not specified.
Title of graph / title=t / This option can be used for displaying a title for the plot. This option is automatically nonexistent if not specified.
Let’s plot a piecewise function:
> f:= piecewise(x<=-2,-x^2,-2<x and x<=2, 2*x,x>2,x^2+4);
plot(f(x), x=-4..4, discont=true);
This should generate the following plot:
Let’s make a plot using as many of the options as possible:
> (cos(x),x=-2*Pi..2*Pi,title=”Cosine Graph”, axes=framed, axiscoordinates=Cartesian,color=green,labels=[x,t],legend=Function);
This input generates the below graph:
Plotting with axes, without axes
There are times when the axes could interfere with displaying certain functions and solutions to ODEs. Fortunately, getting rid of axes in recent versions of Maple is very easy.
One method of specifying axes is to use the above options, but there is also a visual method of changing axes.
Simply right-click the graph that is generated by Maple, and go to the tab labeled “Axes,” and click on “None” to remove the axes from the graph. If you wish to make them appear, make sure the “None” is unchecked. You may also see the axes boxed or framed.
*Note that some options, such as style, point, line, axes, and color, can be altered by right-clicking the graph and changing the details accordingly.
Implicitplot command
*Refer to Maple file “Defining Functions”
There are many plot commands in Maple, and the implicitplot command is very useful for plotting implicitly defined functions (which is very useful for visualizing an ODE problem).
Let’s look at the basic of the syntax of the implicitplot command:
> implicitplot(function, x and y ranges, options);
The implicitplot can be used to plot functions such as circles, functions that involves multiple variables, polar equations, etc. Note that the plots package has to opened.
Let’s plot a circle:
> with(plots):
> implicitplot(x^2+y^2=4,x=-3..3,y=-3..3);
Now let’s try plotting a polar equation:
> implicitplot(r=1-cos(theta), r=0..2, theta=0..2*Pi, coords=polar);
This generates the following plots:
Circular Plot Polar Plot
As you can see, you can practically plot any implicit function using the implicitplot command. Explicit functions can be plotted using the regular plot command.
Displaying multiple plots in the same/separate
*Refer to Maple file “Defining Functions”
Drawing multiple plots is easy, and displaying them on the same or separate plan just adds another step. In order to display them on the same plane, use the display command. The syntax is as follows (for three functions):
> display({function1, function2, function3}, options);
The functions, to prevent human error, can be expressed by variables by defining them. The options listed above can be used for this command as well. Note that the plots package has to be opened as well.
For example:
> with(plots):
> F:=plot(sin(x),x=-Pi..Pi.y=-Pi..Pi,color=blue):
> G:=plot(cos(x),x=-Pi..Pi,y=-Pi..Pi,style=point,color=green):
> H:=plot(tan(x),x=-Pi..Pi,y=-Pi..Pi,style=line):
> display({F,G,H},axes=framed,scaling=constrained,title=”Sine and Cosine”);
This generates the following graph:
Displaying multiple plots separately can be done in one of two ways. You may either plot each function independently, or you may use the Array command. Using the F, G, and H plots above, we can display the plots separately:
> A:=Array(1..3):
> A[1]:= F:
> A[2]:= G:
> A[3]:= H:
> display(A);
Note that the options of the graphs F, G, and H are carried over from the implicitplot command. If you wish to revert the options of each plot, it’s best to enter the restart command and fix the options where the plots F, G, and H are defined.
*NOTE: If you wish to copy the output of Maple to enter into a word processor, make sure to change the font of the output to monospaced, because the default output of Maple is an image and not text.
For implicit plotting, a similar syntax can be used:
c implicitplot([x^2-y^2=2,y=exp(x)+2],x=-Pi..Pi,y=-Pi..Pi,color=[red,black],legend=[function1,function2]);
This generates the following plot:
As you can see in the input, there are a couple of things to note:
- Exponential functions, which is normally expressed as ex, is inputted as “exp().”
- When displaying multiple functions (this applies to the normal plot command as well), you must combine them using brackets []. Any subsequent options are applied in the order respective to the initial input. In this example, the first function x2-y2=2 is red and is labeled function1 because the color and legend options specify those options in the brackets.
2. Solving ODEs: dfieldplot, DEplot, odeplot, Defining Function as Solution, Nullclines
Solving Using Direction/Slope Fields (dfieldplot)
*Refer to Maple file “Direction Fields”
When solving ODEs, there are many methods in plotting them.
In this section, we will learn how to use three plot commands in the DEtools package to plot the solutions to ODEs.
The dfieldplot command draws out a direction/slope field of the given function. The generic syntax of the command is as follows:
> with(DEtools):
> dfieldplot(differential equation, independent variable, x range, y range);
In order to express a differential equation, for example a function of y in relation to x, you must enter “diff(y(x),x).” In some tutorials, this can be expressed as “D(y)(x),” but for simplicities sake, we will use the former expression.
Try out the following:
> dfieldplot(diff(y(x),x)=y(x)*(1-y(x)), y(x), x=-2..2, y=-2..2);
The input should generate the above direction field.
Unfortunately, you must plot differential equations using dfieldplot explicitly. Therefore, it is best to solve a given differential equation to express it explicitly if it is given implicitly.
Solving Using DEplot
*Refer to Maple file “Direction Fields”
If you wish to plot the solutions to the differential equations in a slope field, you must use the DE plot command. The parameters to enter are the same as the dfieldplot command, but includes the initial values and any options.
Therefore, the generic syntax of the DEplot command would be:
> DEplot(differential equation, independent variable, x range, y range, Initial Value(s), Line Color);
Try the following example which involves Newton’s law of cooling:
> ode:=diff(y(t),t)=k*(Am-y(t)): Am:=20: k:=0.1:
> ivs:= [y(0)=20, y(0)=30, y(0)=50]:
> DEplot(ode,y(t),t=0..50,y=0..100,ivs,linecolor=blue);
The input should generate the above direction/slope field with three curves approaching y(t)=20. Note that the initial values used for the DEplot command can be in the form of an array using brackets. Unfortunately, for DEplot, like dfieldplot, the differential equations must be expressed explicitly.
Solving Using odeplot
*Refer to Maple file “Direction Fields”
The odeplot command is slightly different than the DEplot, and is slightly enhanced compared to the dfieldplot. Compared to other plot commands, odeplot has the ability to animate the solution to an ODE. Note that the odeplot command is within the plot package, not the DEtools package.
The generic syntax of the odeplot command is fairly easy and is as follows:
> odeplot(dsolve({differential equation, initial value}, type=numeric, range)
The dsolve command solves the differential equation. Plotting the result will give us a graph of the solution to the ODE. The type=numeric option finds a numerical solution for the differential equation.
Let’s see this in a simple example:
> with(plots):
> ode:=dsolve({diff(y(x),x)=y(x),y(0)=1},type=numeric, range=-5..2):
> odeplot(ode);
This should generate the following graph:
Plotting Vertical Lines
*Refer to Maple file “Vertical Line”
Plotting vertical lines is fairly straightforward and easy. Since vertical lines are expressed with constants in terms of x, simply use the implicitplot command and input the equations.
For example, let’s draw one horizontal line at y=1 and one vertical line at x=Pi/2:
> with(plots):
> a:=implicitplot(y=1,x=-3..3,y=-2..2);
> b:=implicitplot(x=Pi/2,x=-3.33,y=-1..2, color=yellow, linestyle=3,thickness=2);
> display(a,b);
This should result in the following graph:
Note that the linestyle option changes the type of line drawn, and the thickness adjusts the thickness of the specific line. For the linestyle option, 1=solid line, 2=dots, 3=dashes, 4= dashes and dots, 5= bold dashes, etc.
3. Analytical Solutions (General and Particular Solutions) and Cauchy Problem
Deriving analytical solutions in Maple is a cumulative process. This means that the more specific the answer is that you’re looking for, the more steps you have to take to derive that answer. In general, deriving analytical solutions is a two step process, which is solving for a general solution and then solving for the particular solution.
Solving ODEs (General Solution)
Analytical solutions are one of the easiest topics to do in Maple, since Maple will do most of the work for you. All you need to know is the differential equation and any initial conditions it may have to obtain the general and particular solution.
In order to find the general solution, first define the ODE, and then use the dsolve command.
For example:
> ode:= diff(y(x),x)=2*y(x)+10;
> dsolve(ode)
As you can see, the _C1 seen in the output of the second command line represents a constant, which would be normally written as “+C.”
The equation of y(x) is the general solution to the ODE.
Solving ODEs (Particular Solution)
Solving for a particular solution requires the same procedure as solving for general solutions, except it requires including initial conditions.
First, define the initial conditions, then use the dsolve command to solve the ODE subject to the initial conditions.
Let’s use the above ODE for this example:
> ics:=y(0)=1;
> dsolve({ode,ics});
Initial Value Problems (Cauchy problems)
Initial value problems, such as Cauchy problems, can be solved analytically.
Let’s first solve a simple separable equation:
Problem: Consider the following initial value problem: y’=x-y, y(0)=1.
Solution: Try the following input in Maple.
> a:=diff(y(x),x)=x-y(x);
> dsolve(a,y(x));
> dsolve({a,y(0)=1},y(x));
As you can see, the general solution in Maple is expressed with a constant represented by _C1. In the particular solution, the initial conditions are applied and provides you an equation of y in terms of x.
Now let’s try a slightly more difficult problem:
Problem: Consider the following initial value problem: y’=(4-2/3x+4y)-1, y(0)=1.
Solution: Try the following input in Maple:
> b:= diff(y(x),x)=(4-2/3*x+4*y(x))^-1;
> dsolve(b,y(x));
> dsolve({b,y(0)=1},y(x));