function I = GaussInterg(fun, type, a, b, tol)
% GaussInterg 用Gauss型求积公式求积分,具体形式由使用者选取
%
% Synopsis: I = GaussInterg(fun, type, a, b)
% I = GaussInterg(fun, type, a, b, tol)
%
% Input: fun = (string) 被积函数的函数名
% type = (string) 具体Gauss求积公式的形式
% a,b = 积分上下限,Laguerre只计算0到inf,Hermite只计算-inf到inf,所以a,b对这两种形式无效
% tol = (optional) 误差容忍限度,默认为5e-5
%
% Output: I = 通过Gauss型求积公式求积分的近似值
if nargin < 5
tol = 5e-5;
end
n = 7; %默认从7节点多项式开始计算
IOld = 1; %初始化IOld为1
err = 1; %初始化误差为1
while err >= tol
switch type
case 'Legendre' %计算无奇点被积函数在-1到1的积分
I = GaussLegendreInterg(fun, a, b, n);
case 'Chebyshev' %计算被积函数形如 f(x)/sqrt(1-x^2)在-1到1的积分
I = GaussChebyshevInterg(fun, a, b, n);
case 'Laguerre' %计算被积函数形如 exp(-x)*f(x)的在0到inf的积分
I = GaussLaguerreInterg(fun, n);
case 'Hermite' %计算被积函数形如 exp(-x^2)*f(x)的在-inf到inf的积分
I = GaussHermiteInterg(fun, n);
otherwise
error('No such type!');
end
err = abs(I - IOld); %计算误差
IOld = I; %把IOld赋值为I进行下次迭代
n = n+1; %多项式节点递增
end