Maple/Getting Started
Chapter 1: Getting Started with Maple
editBefore You Begin
editAll the commands given to Maple are in the areas marked with >.
These are the input areas.
Press enter to execute the command.
> sample code;
The lines of code must end with either a semicolon or a colon. Note the difference between them:
> p - ln( p );
> p - ln (p ):
If the line of code ends with a semicolon, the order is validated and the software displays the answer in the output area. If the line of code ends with a colon, the order is validated, but the answer is not displayed.
Simple computations with Maple
edit> (1-1/7)*(1+2/3)^2;
> (25)^(1/2);
> factorial(15);
> 15!;
> sqrt(81);
> sqrt(56.81);
> %^2;
Using variables with Maple
editLike any other programming language, Maple allows us to assign values to named variables. This often makes programs more readable and it allows us to save results for use later in the calculation. Here, 2.14 is assigned to the variable p. We can then compute so that p is evaluated (replaced by) 2.14:
> p:=2.14;
> p - ln(p);
You can also use more than one variable in a single expression:
> x := 2*a-p;
> x-sqrt(a*p);
> expr := a*x^2 + b*x + c = 0;
Commands to manipulate expressions in Maple
editMaple has a wide range of commands to help you manipulate expressions:
> f := (a+b)^6;
> expand(f);
> factor(%);
> %%;
> sin(x)^2+cos(x)^2;
> simplify(%);
Here, the % symbol recalls the last computation made by Maple. %% means you recalls the computation made two steps back. %%% will recall the answer to the computation make three steps back. If you wish to recall answers father back, you will have to have assigned them to a variable.
You can also substitute values for the variables in an expression with the commands subs and two-argument eval:
> subs( x=2, x^2+x+1 );
> eval( x^2+x+1,x=2 );
The command subs makes syntactic substitution,
> subs( y=0, sin(y) );
while eval performs substitution followed by evaluation
> eval( sin(y), y=0 );
> eval(f, a=c);
> eval(f, [a=2, b=4/5]);
The command subs substitutes bound (dummy) variables while eval does not:
> subs(x=u,int(f(x),x=0..1) );
> eval(int(f(x),x=0..1),x=u );
The command eval does simultaneous substitution while the command subs does sequential substitution:
> subs( x=y, y=x, [x,y] );
> eval([x,y], {x=y, y=x});
The following are equivalent:
Simultaneous substitution
> subs( [x=y, y=x], [x,y] );
> eval([x,y], {x=y, y=x});
and
Sequential substitution
> subs( x=y, y=x, [x,y] );
> eval(eval([x,y], x=y), y=x);
The command evalf calculates a floating point (numeric) approximation of an expression. You may also specify the desired precision.
> evalf(sqrt(3));
> evalf(sqrt(3), 50);
> evalf[50](sqrt(3));
print, lprint and printf are commands used to display a mathematical expression. print displays the expression using the current pretty-printing method. lprint displays the expression in 1D format (no pretty-printing), as one would type as input to Maple. printf is the same function used in C or C++ language, where you can display variables using formats: %d represents an integer variable, %f a floating number and \n breaks a line of text:
> f:=a-3/a+1/(a*a+1);
> print(f);
> lprint(f);
a-3/a+1/(a^2+1)
> printf("%d %f \n",123,1234/567);
123 2.176367
Defining a function with Maple
editThe simplest form to define a function uses the arrow (map) operator ->
> f := t -> sin(t) - t;
> f(3*x + 2);
Note:
> f := x->2*x+2;
defines f as a function, i.e.
> f(u);
> f(x):=2*x+2;
defines f as a procedure with a "remember table" that is assigned only for the argument x:
> f(u);
and
> f(x)=2*x+2;
is an equation.
Calculus with Maple
editDerivatives of expressions can be taken with the diff command:
> diff(f(t), t);
Multiple partial derivatives can be given as a list. To take the derivative of g with respect to u then v:
> diff(g(u,v,w), [u,v]);
Maple can also take the derivative of functions using the D operator
> D(f);
If a function has more than one variable, the variable can be specified as an index:
> D[1](g);
Notice the difference between diff and D: diff operates on an expression, and returns an expression while D operates on a function and returns another function.
Maple can be used to find a primitive, indefinite integral or anti-derivative of an expression. For example, to get indefinite integrals of the function f(t) according to t and the function g(u,v,w) according to v, the following commands can be entered:
> Int(f(t), t) = int(f(t), t);
> Int(g(u,v,w), v) = int(g(u,v,w), v);
Note the difference between the commands Int and int. The former is the inert form, it just displays the integral. The latter is the active form and computes its value.
Maple can also compute definite integrals:
> Int(f(t),t=0..Pi)=int(f(t),t=0..Pi);
To evalue an inert integral, one can use the command value:
> value(Int(f(t), t));
The other use of this inert form is the numerical approximation of definite integrals:
> evalf(Int(f(t),t=0..Pi));
This should give the same answer as evalf(int(f(t), t=0..Pi)) but it is computed differently. When the inert form of the integral is used Maple computes an approximation of the area under the curve using the definition of definite integration. When then int form is used, and exact value for the definite integral is computed, and then that value is numerically approximated. Using the inert form to compute the approximation directly can be much faster for large problems.
Limits, sums and products with Maple
editYou can also find numeric and symbolic values of sums, limits and products using respectively the functions sum, limit and product:
> limit((2*t-3)/(3*t+4),t=infinity);
> limit((2*t-3)/(3*t+4),t=-4/3,right); #right-bound of the limit
> Sum(i^2,i=1..10)=sum(i^2,i=1..10);
> Product(1/i,i=1..10)=product(1/i,i=1..10);
The commands Sum and Product are inert, they do not actually compute the sum or the product.
Solving equations with Maple
editThe solve(eqn,x) function tries to find the value of x in eqn. You can also use more variables or more equations using solve({eqn1,eqn2,...},{x,y,...}):
> solve(2*t+3=-t+6*sqrt(2));
> solve(t-15/4*u=5/2*(u-t)+3,u);
> solve({a-b=2,a+3*b=7},{a,b});
Sometimes, the exact solution of an equation might not be found. You can then use the fsolve() to get a numerical approximation:
> fsolve(cos(t)=t);
> fsolve({t^3+u=1,u-(t-1)^3=t},{t,u});
Giving names to solutions
editThe solution can be stored in a variable, as any other expression
> solution := solve({a-b = x, a+b = 1/x}, {a, b});
In the example, variable solution is assigned value, than can be used in following calculations. Sometimes, it worth to keep parts of the solution in independent variables. It can be done with appropriate substitution. For the ability to reproduce each step of calculus, it is better not to replace variables (a,b) used to write the original equaitons, but create the new names, for example, solutiona" and "solutionb":
> solutiona := eval(a, solution)
> solutionb := eval(b, solution)
Then, these expressions can be accessed through their identifiers in some plots or future calculation.
Plotting functions with Maple
editTo plot a function f(x), use the command plot(f(x),x):
> plot(sin(t)/t,t=-20..20,title="function t ---> sin t / t");
> plot3d(x*exp(-x^2-y^2),x=-2..2,y=-2..2,color=x,orientation=[120,75]);