Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

An Introduction to Maple Software

Srinivasarao Thota
Department of Mathematics
Motilal Nehru National Institute of Technology
Allahabad, India
srinithota[at]ymail.com

National Conference on
Advances in Mathematical Sciences
Motilal Nehru National Institute of Technology
Allahabad, India

October 05-07, 2012


S.Thota () An Introduction to Maple AMS 2012, Allahabad 1 / 21
Outline

1 Introduction
2 Expressions
3 Functions
4 Solutions of Equations
5 Solving a System of Equations for Several Unknowns
6 Solving Differential Equations
7 Procedures, Variables, and Extending Maple
8 Implementing Packages using Modules
9 Three-dimensional plotting
10 Create a 2-D or 3-D animation on one parameter
11 Maple Help
12 References

S.Thota () An Introduction to Maple AMS 2012, Allahabad 2 / 21


Introduction

1 Maple is a commercial computer algebra system. It was first developed in


1980 by the Symbolic Computation Group at the University of Waterloo in
Waterloo, Ontario, Canada.
2 Since 1988, it has been developed and sold commercially by Waterloo Maple
Inc. (also known as Maplesoft), a Canadian company based in Waterloo,
Ontario. The current major version is version 16 which was released in
March 2012.
3 We can enter mathematics in traditional mathematical notation and custom
user interfaces can also be created. There is support for numeric
computations, to arbitrary precision, as well as symbolic computation and
visualization.
4 There are interfaces to other languages (C, C#, Fortran, Java, MATLAB,
and Visual Basic). There is also an interface with Excel.

S.Thota () An Introduction to Maple AMS 2012, Allahabad 3 / 21


Expressions

To assign an expression to a variable, use :=

> x := 9; variable x is 9 > w := x+3; variable w is 12


> y := x/2; variable y is 9/2 > z := y 3 ; variable z is 729/8
> q1 := 5*w; variable q1 is 60 > q2 := sqrt(x); variable q2 is 3

To reset all quantities

> restart;

To plot an expression

say f = x 2 + cosx on interval [−2, π]


> f:=x∧ 2+cos(x); > plot(f,x=-2.0..Pi,title=‘Srinivas‘);

Substitute into a multivariate expression

say f = x2 + xy
> f:=x2 +x*y; defines f as an expression
> r:=subs(x=1,y=3,f); substitutes x = 1 and y = 3 into f.
S.Thota () An Introduction to Maple AMS 2012, Allahabad 4 / 21
Functions

Define f as a function rather than as an expression

say f(x) = x3 - 3x2 - 9x + 6


> f := x -> x3 - 3*x2 - 9*x + 6 ; f is defined as a function
> f(1); evaluates f at x = 1 and returns -5
> f(x+h); returns (x+h)3 - 3 (x+h)2 - 9 x - 9 h + 6
> plot(f(x), x = -4.0 .. 5.0); plots f(x) from x = -4 to 5

Define a multivariate function.


> f := (x,y) -> y * cos(x); f is defined as a function of x and y
> r1 := f(0,2); evaluates f(0,2) and stores result 2 in r1.
> f(Pi,2); evaluates f(π,2) and returns -2.

Convert powers of trig functions to sines and cosines

> Q1 := cos(x)∧ 3 - 4*sin(x)∧ 5; > Q2 := combine(Q1);


> Q3 := sin(x+y); > Q4 := expand(Q3);

S.Thota () An Introduction to Maple AMS 2012, Allahabad 5 / 21


Piecewise Functions.
For example 

 x +5 if x ≤ −1,

 2
f (x) = x +1 if −1 < x < 2,


 1 if x = 2,
7−x if x > 2.

> f:=x->piecewise(x<=-1, x+5, x<2, x2 +1, x=2, 1,x>2, 7-x);

Order
(range 1, function 1, range 2, function 2, range 3, function 3, ...)

> f(-3); evaluates f(-3)


> f(2); evaluates f(2)
> plot(f(x), x = -6.0..8.0, title = ‘A Piecewise Function‘);
plots f(x) on interval [-6,8] and gives the plot a title
> limit(f(x), x = -1,left); evaluates the leftsided limit of f(x) at x = -1
> limit(f(x), x = -1,right); evaluates the rightsided limit of f(x) at x = -1
> limit(f(x), x = -1); evaluates the limit of f(x) at x = -1 (it does not exist)
> limit(f(x), x = 2); evaluates the limit of f(x) at x = 2
S.Thota () An Introduction to Maple AMS 2012, Allahabad 6 / 21
Solutions of Equations

Plot a function to help determine the location of its zeros.

> f := x -> x6 - 3*x5 - 5*x4 + 15*x3 + 4*x2 - 12*x;


> plot(f(x), x = -4.0..4.0);

Solve a function for its zeros.


> f := x -> x6 - 3*x5 - 5*x4 + 15*x3 + 4*x2 - 12*x;
> solve(f(x), x);

Solve a function for its real zeros.


> f := x -> x6 - 3*x5 - 5*x4 + 15*x3 + 4*x2 - 12*x;
> fsolve(f(x), x); tries to find all real zeros
> fsolve(f(x), x = 0..4); tries to find all real zeros between 0 & 4

S.Thota () An Introduction to Maple AMS 2012, Allahabad 7 / 21


Solving a System of Equations for Several Unknowns

For example

Two equations in two unknowns: x 2 + y 2 = 9, x − y = 1.


> eq1 := x2 + y2 = 9; > eq2 := x - y = 1;
> with(plots, implicitplot); loads the implicitplot command

Plots both equations in the xy-plane on the specified range.

> implicitplot({eq1, eq2}, x = -4.0..4.0 , y = -4.0..4.0);

Solve the system symbolically.

> solve({eq1, eq2},{x,y});

S.Thota () An Introduction to Maple AMS 2012, Allahabad 8 / 21


Cont...

Solve the system numerically, but may give only one solution since no
range is specified.

> fsolve({eq1, eq2},{x,y});

Solves the system numerically on the specified range.

> fsolve({eq1, eq2},{x,y},{x = 0.0..3.0, y = 0.0..3.0});

help
> ?implicitplot for more help with the implicitplot command
> ?fsolve for more help with the fsolve command

S.Thota () An Introduction to Maple AMS 2012, Allahabad 9 / 21


Solving Differential Equations

Derivatives of functions
> diff(f,x); gives f’(x),
> diff(f,x,x); gives f”(x) or > diff(f,x$2); gives f”(x),
> diff(f,x,x,x); gives f(3) (x), or > diff(f,x$3); gives f(3) (x),
etc.
Defining an ordinary differential equation.

For example y 00 + 4y 0 + 13y = cos3x


> de:=diff(y(x),x$2)+4*diff(y(x),x)+13*y(x)=cos(3*x);

Solving the ordinary differential equation for y(x).

> Y:=dsolve(de, y(x));

S.Thota () An Introduction to Maple AMS 2012, Allahabad 10 / 21


Cont...

Solving the ordinary differential equation subject to initial conditions.

y” + 4y’ + 13y = cos 3x


y(0) = 1, y’(0) = 0

> de:=diff(y(x),x$2) + 4*diff(y(x),x) + 13*y(x) = cos(3*x);


> with(DEtools):
> Y:=dsolve(de, y(0)=1, D(y)(0)=0, y(x));
> plot(Y, x=0..5); plots the solution Y from x = 0 to 5

S.Thota () An Introduction to Maple AMS 2012, Allahabad 11 / 21


Procedures, Variables, and Extending Maple

Nested Procedures
We can define a Maple procedure within another Maple procedure.

Procedures That Return Procedures


We can create procedures that return procedures by using Maple
evaluation rules.

Local Variables
Local variables can exist after the procedure which created them has
exited. This feature allows a procedure to return a procedure. The new
procedure requires a unique place to store information.

S.Thota () An Introduction to Maple AMS 2012, Allahabad 12 / 21


Cont...

Interactive Input
We can write interactive procedures, querying the user for missing
information or creating an interactive tutorial or a test.

Extending Maple
The Maple software includes useful mechanisms for extending Maple
functionality, which reduce the need to write special purpose procedures.
Several Maple commands can be extended.

S.Thota () An Introduction to Maple AMS 2012, Allahabad 13 / 21


Hello world

Maple programs are called procedures. keyword: proc.

hello:=proc()
print(‘hello world‘);
end;

The procedure could be made a little more interesting as follows.

Wishing:=proc()
local name;
print(‘What is your name? ‘);
name := readline(terminal);
print(‘Hello‘||name);
end;

Here is a simple procedure that takes one argument:

double:=proc(n)
n*2;
end;
S.Thota () An Introduction to Maple AMS 2012, Allahabad 14 / 21
Here is a variant of the procedure that responds in words.

printdouble:=proc(n::numeric)
printf("Double of %a is %.1f.", n, n*2);
end;

if ... then ... else structure.


halfdouble:=proc(n::posint)
if type(n,even) then n/2;
else 2*n; fi;
end;

Calling proc

callingfun:=proc(n::posint)
print(n);
halfdouble(n);
hello();
end;
S.Thota () An Introduction to Maple AMS 2012, Allahabad 15 / 21
Implementing Packages using Modules

One easy application of modules is the implementation of packages. A package is


simply a collection of routines that are collected together in some way.

Example:
MyPackage := module()
export f1, f2;
local loc1;
option package;
f1 := proc() loc1 end proc;
f2 := proc(v) loc1 := v end proc;
loc1 := 2; end module:

Save package (or any other module) to a Maple.

savelib(’MyPackage’);
restart;
with(MyPackage);

S.Thota () An Introduction to Maple AMS 2012, Allahabad 16 / 21


Three-dimensional plotting

Calling Sequence
plot3d(expr, x=a..b, y=c..d, opts)
plot3d(f, a..b, c..d, opts)
plot3d([exprf, exprg, exprh], s=a..b, t=c..d, opts)
plot3d([f, g, h], a..b, c..d, opts)

For example:
plot3d(sin(x+y),x=-1..1,y=-1..1)
plot3d(binomial, 0..5,0..5,grid=[20,20])
plot3d([1,x,y],x=0..2π,y=0..2π,coords=toroidal(10), scaling =
constrained)
plot3d([1,x,y],x=0..2π,y=0..2π,coords=toroidal(10),scaling=contour)
plot3d([1,x,y],x=0..2π,y=0..2π,coords=toroidal(10),colour=x)

S.Thota () An Introduction to Maple AMS 2012, Allahabad 17 / 21


Create a 2-D or 3-D animation on one parameter

Calling Sequence
animate(plotcommand, plotargs, t=a..b, options)
animate(plotcommand, plotargs, t=L, options)

For example:
with(plots):
animate(plot,[Ax2 ,x=-4..4],A=-3..3)
animate(plot,[Ax2 ,x=-4..4],A=-3..3,trace=5,frames=50)
sinewave:=plot(sin(x),x=0..4π):
animate(spacecurve,[[cos(t),sin(t),(2+sin(A))t], t=0..20 opts],
A=0..2π)

S.Thota () An Introduction to Maple AMS 2012, Allahabad 18 / 21


Maple Help

Maple tutorial or get Maple help


1 Help > Maple Help or
2 Help > Take a Tour of Maple or
3 Help > Quick Help or
4 Help > Quick Reference

For help during a Maple session, click the Help

or enter
1 > ?index
2 > ?command

S.Thota () An Introduction to Maple AMS 2012, Allahabad 19 / 21


References

M. B. Monagan, K. O. Geddes K. M. Heal, G. Labahn S. M.


Vorkoetter J. McCarron P. DeMarco ”Maple Advanced Programming
Guide”.
Maple online help: http://www.maplesoft.com/support/help/Maple
/view.aspx?path=module/package.
M. B. Monagan K. O. Geddes K. M. Heal, G. Labahn S. M. Vorkoetter
J. McCarron P. DeMarco: ”Maple Introductory Programming Guide”.
M. B. Monagan K. O. Geddes K. M. Heal G. Labahn S. M. Vorkoetter
J. McCarron P. DeMarco: ”Maple Advanced Programming Guide”.

S.Thota () An Introduction to Maple AMS 2012, Allahabad 20 / 21


THANK YOU

S.Thota () An Introduction to Maple AMS 2012, Allahabad 21 / 21

You might also like