Maple Tutorial 9:
Some Simple Maple Procedures

M.M. Yovanovich

TUTORIAL9.MWS

Some examples of simple Maple procedures.

> restart: with(plots):

> g1:= x->x^2; g2:= z->sqrt(z); g3:= (x,y)-> x^3 + sin(Pi/y);

[Maple Math]

[Maple Math]

[Maple Math]

> g1(5); g2(z); g3(2,3);

[Maple Math]

[Maple Math]

[Maple Math]

> g:= x-> [x^2, sqrt(1-x^3), sin(x/3), 1/sqrt(x)];

[Maple Math]

> g(2); evalf(%);

[Maple Math]

[Maple Math]

> hyp1:= proc(x,y) sqrt(sin(x)^2 + cos(y)^2) end:

> hyp1(2,2); simplify(%);

[Maple Math]

[Maple Math]

> absolute:= proc(x) abs(x) end:

> absolute(-3.5);

[Maple Math]

> f:= proc(k) option remember;
f(k-1) + cos(3^k*x)/2^k end:

> f(0):= cos(x):

> f(1); f(4);

[Maple Math]

[Maple Math]

> s:= proc(x,n) sum(sin(j*x), j = 1..n) end:

> s(x,4); s(Pi/6,6);

[Maple Math]

[Maple Math]

> eq1:= proc(Bi) x*BesselJ(1,x) - Bi*BesselJ(0,x) = 0 end:

> rt1:= proc(Bi) fsolve(eq1(Bi), x=0..2.45) end:

> rt1(1e-3); rt1(1.5); rt1(100);

[Maple Math]

[Maple Math]

[Maple Math]

> vals1:= [seq(rt1(10^j), j = -6..6)];

[Maple Math]
[Maple Math]
[Maple Math]

> h1:= proc(x) if x < 0 then x else x^2 fi end:

> h1(-3); h1(2);

[Maple Math]

[Maple Math]

Newton-Raphson Method.

> NR:= proc(n) if n = 0 then x0 else
evalf(NR(n-1) - f(NR(n-1))/D(f)(NR(n-1))) fi;
end:

> f:= x->x^3 + 4*x^2 - 6: x0:= 2:

> NR(0); NR(1); NR(2); NR(3); NR(4); NR(5); NR(6);

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Math]

The Newton-Raphson method has converged after 6 iterations to the root with an initial guess of x0 = 2. Maple can find

root easily with the fsolve command.

> fsolve(f(x) = 0, x = 0..2);

[Maple Math]

Trapezoidal Rule.

> f:= 'f': traprule:= proc()
local f, p;
p:= x-> s + t*x:
solve({p(a) = f(a), p(b) = f(b)}, {s,t}):
subs(%, p(x)):
simplify(int(%, x = a..b)):
factor(%) end:

> traprule();

[Maple Math]

The trapezoidal rule formula for one interval is [Maple Math] .

>