The Most Common Maple Commands
The following commands will be used repeatedly throughout the calculus labs. You should know the syntax of these commands.
Variable Assignment
Numbers, expressions and equations can be given variable names in Maple. This reduces typing and also increases accuracy. Assigning expressions to a variable in Maple is accomplished with the ":=" (colon equals) keystrokes. A variable name in Maple may be a single character or a name with more than one character. Variable names should begin with a letter. Examples include the following:
| > | k:=3.76; |
| > | f1:=sqrt(x^2+3*x+7); |
| > | eq1:=x^2+7*x-3; |
When the name of an assigned variable is used in another Maple command, Maple will automatically make the substitution.
Defining a Function
Maple has a special command for defining a function of a single variable, say x . (Any letter can be used for the variable.) The Maple format for defining a function makes a lot of sense if you remember that a function is a rule that begins with an input value, x , and assigns to x an output value. The name of a function can have more than one character. Examples include the following:
| > | f:=x->x^2; |
| > | f1:=x->x/(x+1); |
Once a function has been defined in Maple, you can use standard functional notation such as f ( x ) or f (2).
Clearing all Variables
When beginning a new worksheet or a new section within a given worksheet, it is often useful to remove all variable and function assignments. This is accomplished with the restart command.
| > | restart; |
You can also clear a single variable. Suppose a has been assigned the value 3.
| > | a:=3; |
| > | a: |
If one wants to remove this assignment, the followng command works:
| > | a:='a'; |
| > | a; |
Now a is a free variable with no assigned value.
Solving an Equation
Maple has two very different methods of solving equations. One is algebraic and the other is numerical. To solve an equation algebraically, one uses the solve command. To solve an equation numerically, one uses the f solve command. Both commands require an equation and the variable or variables being solved for. Some examples of the solve and fsolve commands are shown below:
| > | solve(x^2=2,x); |
| > | fsolve(x^2=2,x); |
Often when using the fsolve command, one specifies an interval of x -values over which Maple should search for a solution.
| > | fsolve(x^2=2,x=-2..-1); |
| > | solve(a*x^2+b*x+c=0,x); |
| > | solve(a*x^2+b*x+c=0,a); |
| > | eq1:=x^2+7*x-3; solve(eq1,x); |
If the first argument in either a solve or fsolve command is merely an expression without an equals sign, Maple automatically interprets it as an equation in which the expression is being set equal to 0.
| > | solve(x^2-3,x); |
Plotting Functions
The plot command in Maple is very useful. The basic plot command has three components - the expression being plotted, the x -range and the y -range:
| > | plot(x^2,x=-5..5,y=-20..20); |
The above command instructs Maple to plot the function
within the viewing window [-5,5] by [-20,20]. If the
y
-range is omitted, Maple will automatically set the
y
-range to the numerical range of the function over the
x
-range. This is often a problem when the
y-
range is large.
| > | plot(1/(x-1),x=-5..5); |
| > | plot(1/(x-1),x=-5..5,y=-20..20); |
More than one function cab be plotted in the same window by listing the functions in either square or curly brackets separated by commas.
| > | plot([(x-1)^2,4-x^2],x=-5..5,y=-20..20); |
Variable names of expressions as well as functional notation can be used within a plot command.
| > | f:=x->4-x^2;g:=x->(x-1)^2; plot({f(x),g(x)},x=-5..5,y=-20..20); |
There are may options that can be added to a plot command that control various attributes of the plot such as color, thickness, axes. You can learn about using these options by looking up plot in the Maple help window. The easiest way is to type a keyword such as plot preceded by a question mark on a Maple command line.
| > | ?plot; |
| > | ?plot,color; |
To plot a function given implicitly by an equation in two variables,
for example, first load the
plots
package and then use the
implicitplot
command.
| > | with(plots): |
| > | implicitplot(x^2+y^2=4,x=-3..3,y=-3..3,scaling=constrained); |
Note that Maple automatically cuts down the viewing window. If the option scaling=constrained is not included, the circle is distorted and looks like an ellipse. More than one curve can be plotted by putting the equations within curly braces "{}" and separated by commas.
| > | implicitplot({x^2+y^2=4,1.5*x^2+5*y^2=8},x=-4..4,y=-4..4,scaling=constrained); |
Sometimes an implicit plot can be improved by increasing the number of points in the grid. The standare number of grid points is 25 in each direction.
| > | implicitplot({x^2+y^2=4,1.5*x^2+5*y^2=8},x=-4..4,y=-4..4,scaling=constrained,grid=[60,50]); |
In the above command 60 points were used in the x direction and 50 points were used in the y direction. This means that 3000 points were used within the specified rectangle. Unfortunately, the implicitplot command does not allow different colors to be specified for different curves.
If you want to plot several curves and assign a different color to each curve, you must create a separate plot for each curve with the desired color specified and then display all the plots.
| > | with(plots):plot1:=implicitplot(x^2+y^2=1,x=-2..2,y=-2..2,color=red): |
| > | plot2:=implicitplot(x^2-y^2=1,x=-2..2,y=-2..2,color=blue): |
| > | display(plot1,plot2); |
The Substitute Command
One often has a formula involving one or more variables, and one wants to substitute values for some or all of these variables. This is accomplished with the
subs
command. Suppose we want to evaluate the expression
when
x
= 4. We can do this as follows
| > | subs(x=4,x^2+2*x-3); |
If more than one substitution is being made, one simply lists the substitutions within curly braces "{}" separated by commas.
| > | subs({a=2,b=5,c=Pi},a*x^2+b*x+c); |
Differentiation of Functions
The following Maple command will determine the derivative of the expression
| > | diff(sqrt(x^2+1),x); |
If f ( x ) is a function that has been defined in Maple, "f(x)" can be used in place of the explicit formula
| > | f:=x->sqrt(x^2+1); diff(f(x),x); |
Higher order derivatives can be computed by repeating the variable
x
n
times where
n
is the order of the derivative being calculated. For example the following command computes the third derivative of
| > | diff(x^7-5*x^5+x^2,x,x,x); |
Instead of typing all the x 's in the above command, one can type " x $ n " where n is the order of the derivative being calculated.
| > | diff(x^7-5*x^5+x^2,x$3); |
If one is using the diff command for the derivative and one wants to evaluate the derivative at a particular value of x , one must use the subs command
| > | subs(x=3,diff(x^7-5*x^5+x^2,x$3)); |
There is a second command for calculating the derivative of a function. The function f must first be defined as a function in Maple. One should think of f as the "name" of the function, which is different from the formula for the function. The derivative of f is the function having the name D(f). One then uses this name for a function just like any other name.
| > | f:=x->x^7-5*x^5+x^2; D(f)(x); |
If one wants to evaluate the derivative of f at x = 3, one simply types D(f)(3) assuming the function f has been defined in Maple.
| > | D(f)(3); |
The
D
command for the derivative allows one to treat algebraic combinations of functions. For example if
and
one can differentiate the product with the command D(f*g)(x).
| > | f:=x->x^2;g:=x->sin(x); D(f*g)(x); |
Note that the composition operator for functions is "@":
| > | D(g@f)(x); |
Integrating a Function
To determine an antiderivative of the function
, use the following command
| > | int(4*x^3+6/(x^2),x); |
To calculate the definite integral of the same expression over the interval [1,3] include the endpoints as a range for x
| > | int(4*x^3+6/(x^2),x=1..3); |
If f ( x ) is a function that has been defined in Maple, then the expression f ( x ) can be used in the integration commands.
| > | f:=x->4*x^3+6/(x^2): int(f(x),x=1..3); |
Maple has a built-in procedure for numerically approximating definite integrals. This is particularly useful when a given function cannot be anti-differentiated.
| > | int(sqrt(2+x^5*sin(x)),x); |
Since Maple returned the integral above, this indicates that Maple is unable to anti-differentiate this function. On the other hand we can numerically approximate the same integral over the interval [1,3] by using the evalf command.
| > | evalf(int(sqrt(2+x^5*sin(x)),x=1..3)); |
Setting Digits
The default number of digits used by Maple during calculations and displayed in floating-point numbers is 10.
| > | evalf(sqrt(Pi)); |
Sometimes one wants to use a greater number of digits. This can be done with the Digits:=n command where n is an integer.
| > | Digits:=20; |
| > | evalf(sqrt(Pi)); |