Arman Akbarian
UNIVERSITY OF BRITISH COLUMBIA
PHYSICS & ASTRONOMY DEPT.

 > #Definition of a Procedure in Maple: hello := proc()

 > print("Hello World");

 > end proc;

 (1)

 > hello();

 (2)

 > f:= proc(x)

 > local a; #a will be a local variable inside the procedure

 > a := 1/3;

 > evalf(x*a);

 > end proc;

 >

 (3)

 > f(5);

 (4)

 > f();

 Error, invalid input: f uses a 1st argument, x, which is missing

 > #Types in Maple: whattype(a);

 (5)

 > whattype(2); g:= x->x^2; whattype(g);

 (6)

 (6)

 (6)

 > whattype(2.1);

 (7)

 > whattype(I);

 (8)

 > whattype(sin(x));

 (9)

 > whattype([1..10]);

 (10)

 > "This is a string";

 (11)

 > whattype(%);

 (12)

 > s:="sin(2.1)+cos(5.1)";

 (13)

 > s;

 (14)

 > parse(s);

 (15)

 > evalf(%);

 >

 (16)

 > evalf(s);

 (17)

 > whattype({1,3,5});

 (18)

 > nops(x+y+1);

 (19)

 > whattype(x+y);

 (20)

 > whattype(1..10); myset:=[seq(1..5,0.5)];

 (21)

 (21)

 > whattype(myset); nops(myset); #nops returns the number of operands, in this case operands are members of the list e:= a > b;

 (22)

 (22)

 (22)

 > a := 1;

 (23)

 > b:= 2;

 (24)

 > whattype(e);

 (25)

 > evalb(e);

 (26)

 >

 > f:=n!;

 (27)

 > type(f,function);

 (28)

 > op(f); #finds the operant of a expression

 (29)

 > op(0,f); #finds the operator of an expression

 (30)

 > op(1,f);

 (31)

 > z:=x+y;

 (32)

 > type(z,function);

 (33)

 > op(z);

 (34)

 > op(0,z);

 >

 (35)

 > op(1,z);

 (36)

 > op(2,z);

 (37)

 > assigned(x);

 (38)

 > assigned(z);

 (39)

 > z;

 (40)

 > unassign(z);

 Error, (in unassign) cannot unassign `x+y' (argument must be assignable)

 > unassign('z');

 > z;

 (41)

 > my_fact := proc(x::integer)

 > local i; # 'i' will be a local variable

 > local result; if x > 0 then

 > result := 1;

 > for i from 1 to x do

 > result := result*i;

 > end do;  result;

 > else

 > error "Invalid argument";

 > end if;

 > end proc;

 (42)

 > my_fact(5);

 (43)

 > my_fact(2.1);

 Error, invalid input: my_fact expects its 1st argument, x, to be of type integer, but received 2.1

 > my_fact(-5);

 Error, (in my_fact) Invalid argument

 > fib := proc(x::integer)

 > local a, b, c;

 > a := 2;

 > b := 3; print(a); print(b); c := 5;

 > while b <= x do

 > c := a + b;   if ( c <= x) then

 > print(c);   else          break;   end if;

 > a := b;

 > b := c;

 > end do;

 > end proc;

 (44)

 > fib(88);

 (45)

 (45)

 (45)

 (45)

 (45)

 (45)

 (45)

 (45)

 (45)

 > #Note that reason we still see 89 is that maple returns the last calculation done in the proc as well

 > my_MAX := proc()

 > local i,m :: numerics; description "finds the maximum of a list of numbers";

 > if nargs=0 then              #nargs is the number of arguments

 > return -infinity;

 > else

 > m:= args[1];               #arguments can be grabbed from args[i] list

 > for i from 2 to nargs do

 > if args[i] > m then

 > m := args[i];

 > end if;

 > end do;

 > return m;                  #explicit return, otherwise maple's procedure will return the value of last statement end if;

 > end proc;

 (46)

 > my_MAX();

 (47)

 > my_MAX(1,4,-3,12,6);

 (48)

 > my_MAX(1,infinity,2);

 (49)

 > my_MAX(2,s,5);

 Error, (in my_MAX) cannot determine if this expression is true or false: 2 < s

 > save my_MAX, "My_Procs.mpl";  #Saves the procedure in a text file

 (50)

 >

 > eval(my_fact);

 (51)

 > #Procedure to integrate a given function numerically:

 > my_int:=proc(f::operator, N:: integer, xmin::float, xmax::float)

 > local i,intf;

 > local x; local dx; dx := (xmax-xmin)/(N-1);

 > x:=Array([seq(xmin+i*dx,i=0..N-1)]);

 > intf:=Array(1..N); intf[1]:=0;

 > for i from 2 to N do

 > intf(i) := intf(i-1) + ( f(x[i])+f(x[i-1]) ) / 2 * dx;

 > end do;

 > return(intf);

 > end proc;

 >

 (52)

 > h:= x -> sin(x);

 (53)

 > intsin:=my_int(h,10000,0.0,evalf(4*Pi,10));

 (54)

 > plot([seq(i*(4*evalf(Pi,10))/9999,i=0..9999)],intsin);

 (55)

 Enter an integer number: 5:

 (56)

 (57)

 (58)

 (59)

 > writedata("data2.txt",C,float);

 > ?writedata

 >

last update: Wed May 11, 2016