Maple Tutorial
Math 2250 - Summer 2007
Working with Maple
It is a good habit to begin with a restart command. It will clear all the memory so you can easily redo things
| > | restart; |
Arithmetic operation
| > | 3+4; |
| > | 6*7; |
| > | (3+4)^2/5; |
Use evalf to compute decimal values
| > | evalf((3+4)^2/5); |
You can specify the number of digits of accuracy by using [ ] after evalf. Below, we evaluate to 3 digits accuracy
| > | evalf[3]((3+4)^2/5); |
Some Important Numbers
The number
. Note that you need to type it with capital P.
| > | evalf(Pi); |
The number e. In Maple, you call it by using the exp function
| > | evalf(exp(1)); |
| > | infinity; |
Defining Functions and Variables
| > | a := 3.5; |
Define a variable/parameter by using ":=" not just "=".
You can just refer back to the variable from now on. Maple will remember that a has the value of 3.5
| > | a; |
It is useful to know how to define a function. You will use this often when trying to graph, when doing calculus (differentiating and integrating) and also while working with differential equations.
| > | f := x -> x*(1-x); |
The arrow is typed with a dash followed by a "greater than" sign (no space between them). This represents the function as an operation, taking x and mapping it to x*(1-x).
Now evaluate f at x=0.5
| > | f(0.5); |
You can study a family of functions by including a parameter which you will need to define i.e give it a numerical value. Below, we use the parameter k.
| > | F := x -> k*x*(1-x); |
Now, define what k is supposed to be. Then change it and see what happens.
| > | k:=1; |
| > | F(0.5); |
| > | k:=3; |
| > | F(0.5); |
Differentiation and Integration
Use the command diff to differentiate an expression.
| > | diff(x^3,x); |
You can also take higher order derivative. For example, take the second derivative
| > | diff(x^3,x,x); |
Specify the n-th derivative with respect to x, by typing x n time. The fifth order derivative of sin(x) is
| > | diff(sin(x),x,x,x,x,x); |
You can also take derivative of a function. For example, define this nasty looking functionand then differentiate it with respect its argument x.
| > | f:= x -> exp(sin(x^5))*x^3; |
| > | diff(f(x),x); |
To integrate, use the command int. Note that below, the integration constant is dropped.
| > | int(t^2,t); |
You can also compute definite integral. For example let's compute the integral below
| > | int(t^2,t=0..1); |
Sometimes, Maple does not know how to do things either. Below for example, Maple just return the exact expression, i.e. it cannot find an elementary function for the anti-derivative.
| > | int(t^3*exp(sin(t)),t); |
You can still compute a numerical approximation for a definite integral
| > | evalf(int(t^3*exp(sin(t)),t=0..1)); |
You can even integrate to infinity for some nice functions.
| > | evalf(int(exp(-x),x=0..infinity)); |
Plotting
First, load the plot library
| > | with(plots): |
Warning, the name changecoords has been redefined
To plot something, use the command plot
| > | plot(x^2,x=-1..1); |
![[Plot]](images/maple_tut_30.gif)
You can also change the color of the plot and put title as shown below. Note that the title need to be put using single quote mark '....' instead of the double quotation mark "
| > | plot(x^2,x=-1..1, color=blue,title='parabola'); |
![[Plot]](images/maple_tut_31.gif)
You can plot multiple curves on the same figure plot by using the square brackets [ ].
| > | plot([x^2, x^3, x], x=-1..1, color=[blue,red,black], linestyle=[SOLID,DASH,DASHDOT],legend=['x2','x3','x']); |
![[Plot]](images/maple_tut_32.gif)
You can plot a function as well
| > | f:= x -> x*(1-x); |
| > | plot(f(x),x=-2..3); |
![[Plot]](images/maple_tut_34.gif)
Solving Differential Equations
On your first assignment, you will learn how to use the dsolve command to solve a differential equation. First, learn how to write a differential equation. You can define an equation using the ":=" like usual.
| > | eqn1:= diff(y(t),t)=3*y(t)+t; |
Now, solve the differential equation. Note that because no initial condition is specified, there is an arbitrary integration constant included.
| > | dsolve(eqn1, y(t)); |
Of course, you can also solve the equation directly without naming it first.
| > | dsolve(diff(y(t),t)=3*y(t)+t, y(t)); |
Note that you have to refer to the argument of your state variable. That is, you need to use y(t) not just y when writing your equation, otherwise, Maple will not solve it for you. See below
| > | dsolve(diff(y(t),t)=3*y+t, y(t)); |
Error, (in ODEtools/info) y(t) and y cannot both appear in the given ODE.
You can solve using an initial condition by defining your problem as a system using the curly bracket {your equation, initial condition}
| > | dsolve({eqn1, y(0)=1}, y(t)); |
Slope Fields
You can easily draw slope fields using Maple
| > | with(DEtools): # load differential eqn library |
| > | eqn1:= diff(y(t),t)=3*y(t)+t; # writing a differential equation |
| > | DEplot(eqn1, y(t), t=0..10, y=0..100, title="slope fields");
# plot slope fields with t from 0 to 10 and y from 0 t0 100 |
![[Plot]](images/maple_tut_40.gif)
| > | DEplot(eqn1, y(t), t=0..10, y=0..100,{[y(0)=0],[y(0)=2]}); # tracing a few solution trajectory
# each initial condition must be put inside [.] |
![[Plot]](images/maple_tut_41.gif)
You can change how things look by using the different option. Run the following few commands.
| > | DEplot(eqn1, y(t), t=0..10, y=0..100,{[y(0)=0],[y(0)=2]}, color = blue );
# changing the color of the arrows |
| > | DEplot(eqn1, y(t), t=0..10, y=0..100,{[y(0)=0],[y(0)=2]}, linecolor = blue );
# changing the colors of the solution curves |
| > | DEplot(eqn1, y(t), t=0..10, y=0..100,{[y(0)=0],[y(0)=2]}, arrows = line );
# changing the type of arrows to lines |
| > | DEplot(eqn1, y(t), t=0..10, y=0..100,{[y(0)=0],[y(0)=2]}, arrows = thick );
# thicker arrows |
| > | DEplot(eqn1, y(t), t=0..10, y=0..100,{[y(0)=0],[y(0)=2]}, dirgrid = [20,20] );
# this is the default arrow size, you can make it bigger by choosing smaller values inside dirgrid as shown next |
| > | DEplot(eqn1, y(t), t=0..10, y=0..100,{[y(0)=0],[y(0)=2]}, dirgrid = [10,10] ); |
| > |
Printing
You can print by clicking on the printer button above (or go to File and select Print)
Choose "Print Command" option and then type
lpr -l -P printername
That "-l" is dash-lower-case-L. Then hit "Print" button on the bottom of the print window
Printer names are
lcb115 in room LCB 115
mc155c in the Math Center
Survival Guide
A few things to remember and what you need to do first when things don't work out
* Command lines should begin with the prompt ">"
* End every command with a semicolon ";" or a colon ":" If you end with a semicolon, you will see a visible output while ending with a colon will suppress your output.
* Save your work before exiting. Better yet, just save your work often.
* When you get an error message, always check if you have the correct number of brackets. Also check if your variable is called correctly; variable name and function name are case-sensitive.
* You can enter explanatory comments in a command line by inserting a "#" Any text to the left of "#" will be disregarded .
* You can use Maple in a text editor mode by clicking the "T" button at the very top. To go back to the command line mode, click on the "[>" button next to the "T" .
* You can get help by clicking the Help button on the right hand corner of the Maple window. Choose "Topic Index" and you can search for what you are looking for. Additionally, you can easily look up how to do certain thing if you already know what the command is. For example, to find out how to use the plot command, just type
> ?plot
A window with all the explanation will pop up.
Answers to Frequently Asked Questions
1. Why does Maple assert that my graph is empty or plot an empty graph?
Possible reasons include
- You typed ``pi" instead of ``Pi" for the number p (3.14159...).
- Some parameter name doesn't match. Maple pays attention to the difference between capital letters and small letters. And to the difference between the number 0 and the letter o.
- You typed = instead of := when defining a parameter.
- You tried to use curly brackets or square brackets in defining a function. Maple only likes regular parentheses.
- You forgot to give your function an argument. You must include the x in
> plot (f(x),x=0..1);
to get Maple to plot the function f.
2. What's the deal with functions and expressions?
An expression is a shorthand name for a combination of letters. A function defines an operation. You can define an expression g by
> g := k*x*(1-x);
and a function f by
> f := x -> k*x*(1-x);
Never use expressions unless you have a very good reason (they have some quirks).
3. How do I type Greek letters?
Type them in English, remembering to leave a space at the end. For example a is typed ``alpha", b is typed ``beta", g is typed ``gamma", l is typed ``lambda", and t is typed ``tau". To set a = 5, type
> alpha := 5;
4. Why does my graph look wrong?
Make sure that you used the right kind of brackets. Remember that a graph of f and g can be plotted together with
> plot ([f(x),g(x)],x=0..1);
If f or g is defined wrong (see 2) it will not be plotted.
5. Why won't my exponential functions compute?
Maple doesn't know what to do with ex. You've got to use the name of the function, exp(x).
6. Why does it say ``syntax error" when my line is perfect?
Move back to it with the mouse or the arrows and hit return to try it again. Maple may just be acting moody.