Programmeren in Maple: veeltermen

versie 1.0 voor Maple V release 5

G.J. Bex, dept. WNI, Limburgs Universitair Centrum

Legendre polynomen

De Legendre polynoom [Maple Math] is gedefinieerd als [Maple Math] of vertaald naar Maple:

> Legendre := proc(x::name,k::nonnegint,xl::list)
local i, xa, n;
n := nops(xl) - 1;
xa := array(0..n,xl);
product((x - xa[i])/(xa[k] - xa[i]),i=0..k-1)*
product((x - xa[i])/(xa[k] - xa[i]),i=k+1..n);
end:

Als voorbeeld berekenen we [Maple Math] in de punten [Maple Math] :

> Legendre(x,2,[xx[0],xx[1],xx[2],xx[3],xx[4]]);

[Maple Math]

Een meer klassieke aanpak zou kunnen geprogrammeerd worden als:

> Legendre2 := proc(x::name,k::nonnegint,xl::list)
local i, n, prod;
n := nops(xl);
prod := 1;
for i from 1 to k do
prod := prod*(x - xl[i])/(xl[k+1] - xl[i]);
od;
for i from k + 2 to n do
prod := prod*(x - xl[i])/(xl[k+1] - xl[i]);
od;
prod;
end:

> Legendre2(x,2,[xx[0],xx[1],xx[2],xx[3],xx[4]]);

[Maple Math]

Chebychev polynomen

Chebychev polynomen kunnen recursief gedefinieerd worden: [Maple Math] met [Maple Math] en [Maple Math] of in Maple:

> Chebychev := proc(x::name,n::nonnegint)
option remember;
if n = 0 then
1;
else
if n = 1 then
x;
else
2*x*Chebychev(x,n-1) + Chebychev(x,n-2);
fi;
fi;
end:

> Chebychev(x,3);

[Maple Math]