Lab Maple8 Tutorial
Lab Maple8 Tutorial
Each Maple command ends with a semicolon ( ; ) or a colon ( : ) and the computer does
not execute the command until the line is entered (by hitting the return key). So,
; or : must follow every command
Maple has Help and the question mark ? inserted in the command line and followed by
the operator, the function, the command … leads to the help page of this. You are
strongly encouraged to use the on-line help, as it is emphasized also below.
Operators ( ?+; )
addition: + subtraction: - multiplication: * Division: /
exponentiation: ^ or ** repetition: $
Constants ( ?Pi; )
π: Pi ℮: exp(1) √ -1: I ∞: infinity
For a defined function f one can compute its value in a given point, or the values of its
derivatives in a given point ( ?D; )
f(1): f(1); f’(1): D(f)(1); f’’(1): (D@@2)(f)(1);
Note that the commands diff, int, limit, expand, factor require expressions for arguments.
If an expression is assigned to a variable f (for example f:=2*x^3-3;) then only diff(f,x);
is correct. In this case do not write f(x)!
But if f is defined as function of variable x (for example f:=x->x^2-5;) then only
diff(f(x),x); is correct since f(x) is an expression this time, while f is a function.
Plot the graph of a real function of one or two real variables ( ?plot; )
Examples: plot(x^2+2*x-1, x=-5..6); #plots the graph of the function f(x)=x^2+2x-1
for x in the interval [-5,6].
Note the graph is a planar curve.
plot({sin(x),cos(x)}, x=-5..6); #plots simultaneously the graphs of the
functions f(x)=sin x and g(x)=cos x for x in the interval [-5,6].
plot3d(x^2+y^2, x=-2..2, y=-2..2); #plots in 3d the graph of the function
f(x,y)=x^2+y^2 for (x,y) in the rectangle [-2,2]x[-2,2].
Note the graph is a surface in 3d.
2D and 3D animation work only after calling the plot package, i.e. first we type
with(plots):
Once we execute the maple command, we need to left click on the picture.
At that point choose Animation and Play.
Examples: animate(exp(-(x + t)^2), x = -10..10, t = 0..5, frames = 50);
animate(sin(x*t),x=-4*Pi..4*Pi,t=0..1,color=red);
animate([r*cos(t), r*sin(t), t = 0..5], r = 0..1, frames = 50);
animate3d(cos(t*x)*sin(t*y), x = -Pi..Pi, y = -Pi..Pi, t = 1..2, color = cos(x*y));
animate3d(t*y^2-x^2+x^4,x=-2..2,y=-2..2,t=0..2);
4
First we show how to introduce a differential equation in Maple. Note that, first we have
to clarify the notations for the unknowns and, respectively, for the independent variable.
An important thing to remember is that, in Maple, we have to write each time the
independent variable.
Examples. The first order differential equation x'=x , where the unknown is the
function x of independent variable t, can be introduced in Maple as follows:
eq1:= diff(x(t),t) = x(t);
Here we chose to assign the differential equation to the variable eq1
The second order differential equation x''+5x'-6x=0 , where the
unknown is the function x of independent variable t, can be introduced in MAPLE as:
eq2:= diff(x(t),t$2)+5*diff(x(t),t)-6*x(t)=0;
In order to find the general solution of each of the two equations we use dsolve
Examples. dsolve(eq1,x(t)); dsolve(eq2,x(t));
Notice that the general solution evidentiate as many arbitrary real constants _C1, _C2,
... as the order of the differential equation.
Notice that there is the possibility to know details on the method used by Maple to
solve the differential equation by setting infolevel[dsolve]:=3 . The default setting is 1.
In the case that we need to work further with the expression of a solution, we need to
assign to a new variable this expression. We explain in the sequel how one can do this.
First it is convenient to assign to a variable the output of the command dsolve, i.e.
sol:=dsolve({diff(x(t),t)-x(t)=0,x(0)=3},x(t));
This output is the equality x(t) = -3*exp(t) , hence we do not have yet the
expression of the solution, which is the right-hand side of this equality.
One can obtain the expression using the rhs command sol1expr:= rhs(sol);
With this expression we can do all the operations explained in Lab1.
We explain the same in the case of an IVP for a system of 2 equations with 2 variables.
sol:=dsolve({diff(x(t),t)-x(t)=0,diff(y(t),t)+y(t)=0,
x(0)=3, y(0)=2},x(t));
The output is x(t) = -3*exp(t), y(t)=2*exp(-t)
One can obtain the expression of x(t) and, respectively y(t), using the rhs command as
follows
xsolexpr:= rhs(sol[1]); ysolexpr:= rhs(sol[2]);
4. Linear Algebra
First load some additional packages: with(Student[LinearAlgebra]):
with(LinearAlgebra): with(linalg):
This is how we compute the determinant, the inverse, the eigenvalues, the eigenvectors,
the characteristic polynomial of a square matrix A:
Determinant(A) or det(A)
inverse(A) or A^(-1)
Eigenvalues(A) the result is a column vector that contains the
eigenvalues of A
lam,P:=Eigenvectors(A) the result gives first a column vector that
contains the eigenvalues of A (the variable named lam contains this
vector) and a matrix (here named P) whose columns are linear
independent eigenvectors of A corresponding to the order of the
eigenvalues as in the vector lam.
CharacteristicPolynomial(A,r) we can specify the name of
the variable, here is r
This is how we compute the Jordan form of a square matrix A, i.e. the “simplest” matrix
similar to A. We have that A is diagonalizable if and only if its Jordan form is diagonal.
JordanForm(A) this is gives the Jordan form of A, denoted B
JordanForm(A, output=’Q’) this is gives the transition (or
similarity matrix), i.e. the invertible matrix P such that A=PBP^(-1). We
know that, when A is diagonalizable, the columns of P are linearly
independent eigenvectors of A.
This is how we compute the exponential of a matrix A: MatrixExponential(A)
This is how we compute the limit as t goes to infinity of exp(tA):
Map(limit,MatrixExponential(t*A),t=infinity)
7
Given a set or list of initial conditions, and a system of first order differential equations or
a single higher order differential equation, DEplot plots solution curves, by numerical
methods. The default method of integration is method=rkf45.
Represent simultaneously the solution curves with initial values y(0)=1, y’(0)=0 and,
respectively, y(0)=-1, y’(0)=0 of the differential equation y’’(x)=y(x).
DEplot(diff(y(x),x$2)=y(x), y(x), x=0..1,
[[y(0)=1,D(y)(0)=0],[y(0)=-1,D(y)(0)=0]]);
Represent the orbit corresponding to the initial values x(0)=1, y(0)=0 of the autonomous
system x’(t)=-y(t), y’(t)=x(t) .
DEplot([diff(x(t),t)=-y(t), diff(y(t),t)=x(t)],
[x(t),y(t)], t=0..9, [[x(0)=1,y(0)=0]]);
Represent simultaneously the orbits corresponding to the initial values x(0)=1, y(0)=0,
and, respectively x(0)=1, y(0)=1 of the autonomous system x’(t)=-y(t), y’(t)=x(t) .
9
DEplot([diff(x(t),t)=-y(t), diff(y(t),t)=x(t)],
[x(t),y(t)], t=0..9, [[x(0)=1,y(0)=0],[x(0)=1,y(0)=1]],
linecolor=[green,gold]);
produces a set of level curves of the input function for a discrete set of values (i.e. levels)
of the third coordinate. It work only after loading the package plots: with(plots):
Examples: Represent the level curves of H(x,y)=sin(xy) in the box [-1,1]x[-1,1]
contourplot(sin(x*y), x=-3..3, y=-3..3);
Jm:=Jacobian([f1(x,y), f2(x,y)],[x,y]);
A:=subs([x=p1, y=p2], Jm);
eigenvalues(A);
9. Map iterations
We show how to find the first 30 iterations of a function f starting from x0. Then we represent
them using the command pointplot.
> x:=x0;
> for i from 1 to 30 do x:=f(x): psi(i):=x: print(x); od:
> points:=[[n,psi(n)]$n=1..30]:with(plots): pointplot(points,
symbol=circle);
10
> restart:
> with(DEtools):
> f:=(x,y)->2*x*y+exp(x^2); this is the right-hand side of the differential equation
> dsolve({diff(y(x),x)=f(x,y(x)),y(0)=1});phi:=unapply(rhs(%),x);
> DEplot(diff(y(x),x)=f(x,y(x)),y(x),x=0.. 1.5, [[y(0)=1]],
y=1..25);
> h:=0.1; # this is the stepsize. Hence, the number of steps necessary to cover the interval
[0, 1.5] is 15
> x:=0; y:=1; # (0,1) is the starting point, as given by the initial condition
> for i from 1 to 15 do y:=y+h*f(x,y): psi(i):=y: x:=x+h:
print(x,y,phi(x),abs(y-phi(x))); od:
> points:=[[n,psi(n)]$n=1..15]:with(plots): pointplot(points,
symbol=point);plot(phi(t),y=1..1.5);
We will apply the now the improved Euler's numerical method. We have to change only the
numerical formula. We must restart!
> restart:
> phi:=x->(x+1)*exp(x^2);
> h:=0.1; x:=0; y:=1;
> f:=(x,y)->2*x*y+exp(x^2);
> for i from 1 to 15 do y:=y+h/2*f(x,y)+h/2*f(x+h,y+h*f(x,y)):
psi(i):=y: x:=x+h: print(x,y,phi(x),abs(y-phi(x))); od:
> points:=[[n,psi(n)]$n=1..15]:with(plots): pointplot(points,
style=point);plot(phi(t),t=1..1.5);