这段代码可以给出常用的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
%%正交多项式%%此函数包括勒让德正交多项式,切比雪夫正交多项式(两类),拉盖尔正交多项式,埃尔米特正交多项式,输入项数应从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 endend