這段代碼可以給出常用的4類正交多項式的具體表達式,后續將給出可自定義的任意正交多項式代碼
%%正交多項式
%%此函數包括勒讓德正交多項式,切比雪夫正交多項式(兩類),拉蓋爾正交多項式,埃爾米特正交多項式,輸入項數應從1開始
%%n是多項式的項數,n>=0,type是類型,分為Legendre、Chebyshev、Laguerre、Hermite,對應其正交多項式
function OP = Orthogonal_polynomial(type)
sym type;
if strcmp(type,'Legendre') == 1
disp('目前輸出為勒讓德多項式');
disp('定義區間為:');[-1 1]
N = input('請輸入勒讓德多項式的項數:');
L = Legendre(N);
OP = simplify(L(N));
elseif strcmp(type,'Chebyshev') == 1
disp('目前輸出為切比雪夫多項式');
disp('定義區間為:');[-1 1]
disp('在這里,規定第一類切比雪夫多項式是以1/sqrt(1-x^2)作為權函數,第二類切比雪夫多項式以sqrt(1-x^2)作為權函數得到的');
class = input('請輸入需要輸出第幾類切比雪夫多項式(輸入1,2)即可:');
N = input('請輸入切比雪夫多項式的項數:');
Che = Chebyshve(N,class);
OP = simplify(Che(N));
elseif strcmp(type,'Laguerre') == 1
disp('目前輸出為拉蓋爾多項式');
disp('定義區間為:');[0 +inf]
N = input('請輸入拉蓋爾多項式的項數:');
La = Laguerre(N);
OP = simplify(La(N));
elseif strcmp(type,'Hermite') == 1
disp('目前輸出為埃爾米特多項式');
disp('定義區間為:');[-inf +inf]
N = input('請輸入埃爾米特多項式的項數:');
H = Hermite(N);
OP = simplify(H(N));
end
%%勒讓德多項式
function L = Legendre(N)
x = sym('x');
for i = 1:N
Leg(i) = diff((x^2-1)^(i-1),i-1)/(factorial(i-1)*2^(i-1));
end
L = Leg;
end
%%切比雪夫多項式
function C = Chebyshve(n,class)
x = sym('x');
if class == 1
T = string([1 x]);
T = sym(T);
if n <=2
C = T(1:n);
else
for i = 2:n
T(i+1) = 2*x*T(i)-T(i-1);
end
C = T(1:n);
end
elseif class ==2
U = string([1]);
U = sym(U);
U = [U 2*x];
if n <=2
C = U(1:n);
else
for i = 2:n
U(i+1) = 2*x*U(i)-U(i-1);
end
C = U(1:n);
end
end
end
%%埃爾米特多項式
function H = Hermite(N)
x = sym('x');
for i = 1:N
He(i) = (-1)^N*exp(x^2)*diff(exp(-x^2),(i-1));
end
H = simplify(He);
end
%%拉蓋爾多項式
function La = Laguerre(N)
x = sym('x');
for i = 1:N
Lag(i) = exp(x)*diff(x^(i-1)*exp(-x),(i-1));
end
La = simplify(Lag);
end
%%階乘函數
function F = factorial(n)
if n == 0
F = 1;
else
F = factorial(n-1)*n;
end
end
end
