# Maple/Getting Started

## Chapter 1: Getting Started with Maple

### Before You Begin

All 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 \left(p\right)$
> 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

> (1-1/7)*(1+2/3)^2;

${\frac {50}{21}}$
> (25)^(1/2);

$5\,$
> factorial(15);

$1307674368000\,$
> 15!;

$1307674368000\,$
> sqrt(81);

$9\,$
> sqrt(56.81);

$7.537240874\,$
> %^2;

$56.81\,$

### Using variables with Maple

Like 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 $p-\ln \left(p\right)$  so that p is evaluated (replaced by) 2.14:

> p:=2.14;

$p:=2.14\,$
> p - ln(p);

$1.379194171\,$

You can also use more than one variable in a single expression:

> x := 2*a-p;

$x:=2\,a-2.14$
> x-sqrt(a*p);

$2\,a-2.14-1.462873884\,{\sqrt {a}}$
> expr := a*x^2 + b*x + c = 0;

$expr:={a{x}}^{2}+bx+c=0\,$

### Commands to manipulate expressions in Maple

Maple has a wide range of commands to help you manipulate expressions:

> f := (a+b)^6;

$f:=\left(a+b\right)^{6}$
> expand(f);

${a}^{6}+6\,{a}^{5}b+15\,{a}^{4}{b}^{2}+20\,{a}^{3}{b}^{3}+15\,{a}^{2}{b}^{4}+6\,a{b}^{5}+{b}^{6}$
> factor(%);

$\left(a+b\right)^{6}$
> %%;

${a}^{6}+6\,{a}^{5}b+15\,{a}^{4}{b}^{2}+20\,{a}^{3}{b}^{3}+15\,{a}^{2}{b}^{4}+6\,a{b}^{5}+{b}^{6}$
> sin(x)^2+cos(x)^2;

$\left(\sin(x)\right)^{2}+\left(\cos(x)\right)^{2}$
> simplify(%);

$1\,$

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 );

$7\,$
> eval( x^2+x+1,x=2 );

$7\,$

The command subs makes syntactic substitution,

> subs( y=0, sin(y) );

$\sin(0)\,$

while eval performs substitution followed by evaluation

> eval( sin(y), y=0 );

$0\,$
> eval(f, a=c);

$\left(c+b\right)^{6}$
> eval(f, [a=2, b=4/5]);

${\frac {7529536}{15625}}$

The command subs substitutes bound (dummy) variables while eval does not:

> subs(x=u,int(f(x),x=0..1) );

$\int _{0}^{1}\!f\left(u\right){du}$
> eval(int(f(x),x=0..1),x=u );

$\int _{0}^{1}\!f\left(x\right){dx}$

The command eval does simultaneous substitution while the command subs does sequential substitution:

> subs( x=y, y=x, [x,y] );

$[x,x]\,$
> eval([x,y], {x=y, y=x});

$[y,x]\,$

The following are equivalent:

Simultaneous substitution

> subs( [x=y, y=x], [x,y] );

$[y,x]\,$
> eval([x,y], {x=y, y=x});

$[y,x]\,$

and

Sequential substitution

> subs( x=y, y=x, [x,y] );

$[x,x]\,$
> eval(eval([x,y], x=y), y=x);

$[x,x]\,$

The command evalf calculates a floating point (numeric) approximation of an expression. You may also specify the desired precision.

> evalf(sqrt(3));

$1.732050808\,$
> evalf(sqrt(3), 50);

$1.7320508075688772935274463415058723669428052538104\,$
> evalf(sqrt(3));

$1.7320508075688772935274463415058723669428052538104\,$

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);

$f:=a-{\frac {3}{a}}+{\frac {1}{{a}^{2}+1}}$
> print(f);

$a-{\frac {3}{a}}+{\frac {1}{{a}^{2}+1}}$
> lprint(f);


a-3/a+1/(a^2+1)

> printf("%d %f \n",123,1234/567);


123 2.176367

### Defining a function with Maple

The simplest form to define a function uses the arrow (map) operator ->

> f := t -> sin(t) - t;

$f:=t\rightarrow \sin(t)-t$
> f(3*x + 2);

$\sin(3\,x+2)-3\,x-2$

Note:

> f := x->2*x+2;


defines f as a function, i.e.

> f(u);

$2u+2\,$
> f(x):=2*x+2;


defines f as a procedure with a "remember table" that is assigned only for the argument x:

> f(u);

$\mathrm {f} (u)\,$

and

> f(x)=2*x+2;



is an equation.

### Calculus with Maple

Derivatives 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(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 \left(\sin(t)-t\right)\,dt=-\cos(t)-{\frac {1}{2}}\,{t}^{2}$
> Int(g(u,v,w), v) = int(g(u,v,w), v);

$\int \left({\frac {1}{u}}+e^{u+v}+(u-v+w)^{2}\right)\,{dv}={\frac {v}{u}}+{e^{u+v}}-{\frac {1}{3}}(u-v+w)^{3}$

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);

$\int _{0}^{\pi }\left(\sin(t)-t\right)\,dt=2-{\frac {1}{2}}\pi ^{2}$

To evalue an inert integral, one can use the command value:

> value(Int(f(t), t));

$-\cos(t)-{\frac {1}{2}}\,{t}^{2}$

The other use of this inert form is the numerical approximation of definite integrals:

> evalf(Int(f(t),t=0..Pi));

$1.57079633$

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

You 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);

${\frac {2}{3}}$
> limit((2*t-3)/(3*t+4),t=-4/3,right); #right-bound of the limit

$-\infty$
> Sum(i^2,i=1..10)=sum(i^2,i=1..10);

$\sum _{i=1}^{10}i^{2}=385$
> Product(1/i,i=1..10)=product(1/i,i=1..10);

$\prod _{i=1}^{10}{\frac {1}{i}}={\frac {1}{3628800}}$

The commands Sum and Product are inert, they do not actually compute the sum or the product.

### Solving equations with Maple

The 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));

$-1+2{\sqrt {2}}$
> solve(t-15/4*u=5/2*(u-t)+3,u);

${\frac {14}{25}}t-{12}{25}$
> solve({a-b=2,a+3*b=7},{a,b});

$\left\{a={\frac {13}{4}},b={\frac {5}{4}}\right\}$

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);

$0.7390851332$
> fsolve({t^3+u=1,u-(t-1)^3=t},{t,u});

$\{t=0.6941457205,u=0.6655340191\}$

### Giving names to solutions

The solution can be stored in a variable, as any other expression

> solution := solve({a-b = x, a+b = 1/x}, {a, b});


$solution:=\left\{b=-{\frac {1}{2}}{\frac {x^{2}-1}{x}},~a={\frac {1}{2}}{\frac {x^{2}+1}{x}}\right\}$

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)


$solutiona:={\frac {1}{2}}{\frac {x^{2}+1}{x}}$

> solutionb := eval(b, solution)


$solutionb:=-{\frac {1}{2}}{\frac {x^{2}-1}{x}}$

Then, these expressions can be accessed through their identifiers in some plots or future calculation.

### Plotting functions with Maple

To 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]);