资源描述:
《matlab代码--数值积分》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、数值积分1.CombineTraprl复合梯形公式求积分function[I,step]=CombineTraprl(f,a,b,eps)%f被积函数%a,b积分上下限%eps精度%I积分结果%step积分的子区间数if(nargin==3)eps=1.0e-4;endn=1;h=(b-a)/2;I1=0;I2=(subs(sym(f),findsym(sym(f)),a)+subs(sym(f),findsym(sym(f)),b))/h;whileabs(I2-I1)>epsn=n+1;h=(b-a)/n
2、;I1=I2;I2=0;fori=0:n-1x=a+h*i;x1=x+h;I2=I2+(h/2)*(subs(sym(f),findsym(sym(f)),x)+subs(sym(f),findsym(sym(f)),x1));endendI=I2;step=n;2.IntSimpson用辛普森系列公式求积分function[I,step]=IntSimpson(f,a,b,type,eps)%type=1辛普森公式%type=2辛普森3/8公式%type=3复合辛普森公式if(type==3&&nargin
3、==4)eps=1.0e-4;%缺省精度为0.0001endI=0;switchtypecase1,I=((b-a)/6)*(subs(sym(f),findsym(sym(f)),a)+...4*subs(sym(f),findsym(sym(f)),(a+b)/2)+...subs(sym(f),findsym(sym(f)),b));step=1;case2,I=((b-a)/8)*(subs(sym(f),findsym(sym(f)),a)+...3*subs(sym(f),findsym(sym(
4、f)),(2*a+b)/3)+...3*subs(sym(f),findsym(sym(f)),(a+2*b)/3)+subs(sym(f),findsym(sym(f)),b));step=1;case3,n=2;h=(b-a)/2;I1=0;I2=(subs(sym(f),findsym(sym(f)),a)+subs(sym(f),findsym(sym(f)),b))/h;whileabs(I2-I1)>epsn=n+1;h=(b-a)/n;I1=I2;I2=0;fori=0:n-1x=a+h*i;x
5、1=x+h;I2=I2+(h/6)*(subs(sym(f),findsym(sym(f)),x)+...4*subs(sym(f),findsym(sym(f)),(x+x1)/2)+...subs(sym(f),findsym(sym(f)),x1));endendI=I2;step=n;end3.NewtonCotes用牛顿-科茨系列公式求积分functionI=NewtonCotes(f,a,b,type)%type=1科茨公式%type=2牛顿-科茨六点公式%type=3牛顿-科茨七点公式I=0;s
6、witchtypecase1,I=((b-a)/90)*(7*subs(sym(f),findsym(sym(f)),a)+...32*subs(sym(f),findsym(sym(f)),(3*a+b)/4)+...12*subs(sym(f),findsym(sym(f)),(a+b)/2)+...32*subs(sym(f),findsym(sym(f)),(a+3*b)/4)+7*subs(sym(f),findsym(sym(f)),b));case2,I=((b-a)/288)*(19*subs
7、(sym(f),findsym(sym(f)),a)+...75*subs(sym(f),findsym(sym(f)),(4*a+b)/5)+...50*subs(sym(f),findsym(sym(f)),(3*a+2*b)/5)+...50*subs(sym(f),findsym(sym(f)),(2*a+3*b)/5)+...75*subs(sym(f),findsym(sym(f)),(a+4*b)/5)+19*subs(sym(f),findsym(sym(f)),b));case3,I=((b
8、-a)/840)*(41*subs(sym(f),findsym(sym(f)),a)+...216*subs(sym(f),findsym(sym(f)),(5*a+b)/6)+...27*subs(sym(f),findsym(sym(f)),(2*a+b)/3)+...272*subs(sym(f),findsym(sym(f)),(a+b)/2)+...27*subs(sym(f),find