物探Matlab作业(一)作业要求:用梯形法或者辛普森法数值积分,分别用Matlab和c语言实现。C语言1.程序代码:#includestdio.h#includemath.hdoublefun(doublex){returnx*x;}doubledefinfresult1(double(*pfun)(double),doublea,doubleb,doubleeps){intn=1;doubleh,k,tn,tn1,fh,fh1=0;doublefa=pfun(a);doublefb=pfun(b);tn=(b-a)*(fa+fb)/2;do{for(k=0,fh1=0;kn;k++){h=(b-a)/(n);fh=pfun(a+(2*k+1)*(b-a)/(2*n));fh=fh+fh1;fh1=fh;}tn1=tn;tn=(tn1+fh*h)/2;n=2*n;}while(fabs(tn-tn1)=eps);returntn;}intmain(){doublea,b,eps,definfresult;printf(积分下限a=);scanf(%lf,&a);物探printf(积分上限b=);scanf(%lf,&b);printf(精度eps=);scanf(%lf,&eps);definfresult=definfresult1(fun,a,b,eps);printf(\n计算结果=%.7lf\n,definfresult);}2.运行结果:MatLab1.程序代码:functiony=fun1(x)y=x*x;物探functionresult=definf1(fhandle,a,b,eps)fa=feval(fhandle,a);fb=feval(fhandle,b);tn=(b-a)*(fa+fb)/2;tn1=0;n=1;fh1=0;whileabs(tn-tn1)epsfh1=0;fori=0:n-1h=(b-a)/n;fh=feval(fhandle,a+(2*i+1)*(b-a)/(2*n));fh=fh+fh1;fh1=fh;end;tn1=tn;tn=(tn1+fh*h)/2;n=2*n;endresult=tn;物探result=definf1(@fun1,0,10,0.001)result=333.33352.运行结果