日期:2014 年11月8日
问题 名称 | 编写 Gauss 积分方法的程序,要求附有算例。 | ||||
问题描述: 编写一MATLAB程序,算法为 定步长Gauss法,功能为求函数的积分。积分原理为: 。 | |||||
MATLAB程序: function g=nagsint(fname,a,b,n,m) % fname是被积函数,a,b分别是上下限,n为等分数,m为每段Gauss点数。 switch m case 1 t=0,A=2, case 2 t=[-1/sqrt(3),1/sqrt(3)];A=[1,1]; case 3 t=[-sqrt(0.6),0,sqrt(0.6)];A=[5/9,8/9,5/9]; case 4 t=[-0.861136 -0.339981 0.339981 0.861136]; A=[0.347855 0.652145 0.652145 0.347855]; case 5 t=[-0.906180 -0.538469 0 0.538469 0.906180]; A=[0.236927 0.478629 0.5688 0.478629 0.236927]; otherwise error('本程序Gauss点数只能取1,2,3,4,5'); end % t为Gauss点,A为求积系数。 x=linspace(a,b,n+1); g=0; for i=1:n g=g+gsint(fname,x(i),x(i+1),A,t); end % 子函数 function g=gsint(fname,a,b,A,t) g=(b-a)/2*sum(A.*feval(fname,(b-a)/2*t+(a+b)/2));
| |||||
算例: 求解积分: 在MATLAB的Command Window中输入: >> format long;nagsint(inline('sin(x)./x'),eps,1,2,3),format short; | |||||
计算结果与分析: 计算结果如下: ans = 0.946083071343027 数据分析: 这里使用两段复化三点Gauss公式,已达到8位有效数字的精度。 |