《MATLAB定积分的计算程序.docx》由会员分享,可在线阅读,更多相关《MATLAB定积分的计算程序.docx(2页珍藏版)》请在三一办公上搜索。
1、MATLAB定积分的计算程序课本75页第2题 梯形法 syms x fx fx=1/x; a=1;b=2; n=120; h=(b-a)/n; sum=0; for i=1:n; xi=a+i*h; xj=a+(i-1)*h; fxi=subs(fx,x,xi); fxj=subs(fx,x,xj); sum=sum+(fxi+fxj)*h/2; end sum integrate=int(fx,1,2); integrate=double(integrate) fprintf(The relative error between sum and eral-value is about :%g
2、nn,. abs(sum-integrate)/integrate) 抛物线法 syms x fx fx=1/x; a=1;b=2; n=120; h=(b-a)/n; sum=0; for i=1:n; xi=a+i*h; xj=a+(i-1)*h; xk=(xi+xj)/2; fxi=subs(fx,x,xi); fxj=subs(fx,x,xj); fxk=subs(fx,x,xk); sum=sum+(fxi+4*fxk+fxi)*h/6; end sum integrate=int(fx,1,2); integrate=double(integrate) fprintf(The re
3、lative error between sum and eral-value is about :%gnn,. abs(sum-integrate)/integrate) trapz与quad的比较 x=1:1/120:2; y=1./x; trapz(x,y) quad(1./x,1,2) 76页第六题 n=100;a=0;b=1; syms x fx fx=1/(1+x2); h=(b-a)/n; i=1:n; xj=a+(i-1)*h; xi=a+i*h; xij=(xi+xj)/2; fxj=subs(fx,x,xj); fxi=subs(fx,x,xi); fxij=subs(fx,x,xij); f1=fxj*h; f2=fxi*h; f3=fxij*h; inum1=sum(f1) inum2=sum(f2) inum3=sum(f3)