欢迎来到天天文库
浏览记录
ID:39482199
大小:33.50 KB
页数:3页
时间:2019-07-04
《变步长复化辛普森公式计算积分》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、2.编写用变步长复化辛普森公式计算积分的程序。用上面编写的程序计算下列积分并分析计算结果(1)(2)(3)程序:functionS=bianfuhuasimpson(fx,a,b,eps,M)%变步长复合simpson求积公式%调用方式:S=fuhuasimpson(@fx,a,b,epsilon)%fx--求积函数(函数文件)%a,b--求积区间%eps--计算精度%M--最大允许输出划分数n=1;h=(b-a)/n;T1=h*(feval(fx,a)-feval(fx,b))/2;Hn=h*feval(fx,(a
2、+b)/2);S1=(T1+2*Hn)/3;n=2*n;%最好与倒数第三行保持一致(变步长)whilen<=MT2=(T1+Hn)/2;Hn=0;h=(b-a)/n;forj=1:nx(j)=a+(j-1/2)*h;y(j)=feval(fx,x(j));Hn=Hn+y(j);endHn=h*Hn;S2=(T2+2*Hn)/3;fprintf('n=%2dS2=%-12.9fS2-S1=%-12.9f',n,S2,abs(S2-S1));ifabs(S2-S1)3、n=2*n;endendS=S2;%达到下列条件之一,则运算终止:%(1).abs(S2-S1)M%输入1:S=bianfuhuasimpson(inline('sqrt(x)*cos(x)'),0,pi,10e-6,2000)%输入2:S=bianfuhuasimpson(inline('2*x^2*cos(x^2)'),0,sqrt(pi),10e-6,2000)%输入3:S=bianfuhuasimpson(inline('sqrt(x)'),0,1,10e-6,2000)输出结果4、:(1)S=bianfuhuasimpson(inline('sqrt(x)*cos(x)'),0,pi,10e-6,2000)n=2S2=-0.016369112S2-S1=0.944423778n=4S2=-0.450266122S2-S1=0.433897010n=8S2=-0.669839370S2-S1=0.219573248n=16S2=-0.781318443S2-S1=0.111479074n=32S2=-0.837710689S2-S1=0.056392245n=64S2=-0.866141900S5、2-S1=0.028431211n=128S2=-0.880440980S2-S1=0.014299080n=256S2=-0.887620063S2-S1=0.007179083n=512S2=-0.891220052S2-S1=0.003599989n=1024S2=-0.893023740S2-S1=0.001803689S=-0.8930(2)S=bianfuhuasimpson(inline('2*x^2*cos(x^2)'),0,sqrt(pi),10e-6,2000)n=2S2=1.076354541S6、2-S1=2.092222287n=4S2=0.039359358S2-S1=1.036995183n=8S2=-0.430456535S2-S1=0.469815894n=16S2=-0.662796649S2-S1=0.232340113n=32S2=-0.778823323S2-S1=0.116026674n=64S2=-0.836827971S2-S1=0.058004648n=128S2=-0.865829756S2-S1=0.029001785n=256S2=-0.880330615S2-S1=0.0147、500859n=512S2=-0.887581042S2-S1=0.007250427n=1024S2=-0.891206256S2-S1=0.003625214S=-0.8912(3)S=bianfuhuasimpson(inline('sqrt(x)'),0,1,10e-6,2000)n=2S2=0.489859598S2-S1=0.185121744n=4S2=0.579745947S2-S1=0.089886349n=8S2=0.623731522S2-S1=0.043985575n=16S2=0.645388、4849S2-S1=0.021653327n=32S2=0.656091436S2-S1=0.010706587n=64S2=0.661402273S2-S1=0.005310836n=128S2=0.664042680S2-S1=0.002640407n=256S2=0.665357576S2-S1=0.001314896n=512S2=0.
3、n=2*n;endendS=S2;%达到下列条件之一,则运算终止:%(1).abs(S2-S1)M%输入1:S=bianfuhuasimpson(inline('sqrt(x)*cos(x)'),0,pi,10e-6,2000)%输入2:S=bianfuhuasimpson(inline('2*x^2*cos(x^2)'),0,sqrt(pi),10e-6,2000)%输入3:S=bianfuhuasimpson(inline('sqrt(x)'),0,1,10e-6,2000)输出结果
4、:(1)S=bianfuhuasimpson(inline('sqrt(x)*cos(x)'),0,pi,10e-6,2000)n=2S2=-0.016369112S2-S1=0.944423778n=4S2=-0.450266122S2-S1=0.433897010n=8S2=-0.669839370S2-S1=0.219573248n=16S2=-0.781318443S2-S1=0.111479074n=32S2=-0.837710689S2-S1=0.056392245n=64S2=-0.866141900S
5、2-S1=0.028431211n=128S2=-0.880440980S2-S1=0.014299080n=256S2=-0.887620063S2-S1=0.007179083n=512S2=-0.891220052S2-S1=0.003599989n=1024S2=-0.893023740S2-S1=0.001803689S=-0.8930(2)S=bianfuhuasimpson(inline('2*x^2*cos(x^2)'),0,sqrt(pi),10e-6,2000)n=2S2=1.076354541S
6、2-S1=2.092222287n=4S2=0.039359358S2-S1=1.036995183n=8S2=-0.430456535S2-S1=0.469815894n=16S2=-0.662796649S2-S1=0.232340113n=32S2=-0.778823323S2-S1=0.116026674n=64S2=-0.836827971S2-S1=0.058004648n=128S2=-0.865829756S2-S1=0.029001785n=256S2=-0.880330615S2-S1=0.014
7、500859n=512S2=-0.887581042S2-S1=0.007250427n=1024S2=-0.891206256S2-S1=0.003625214S=-0.8912(3)S=bianfuhuasimpson(inline('sqrt(x)'),0,1,10e-6,2000)n=2S2=0.489859598S2-S1=0.185121744n=4S2=0.579745947S2-S1=0.089886349n=8S2=0.623731522S2-S1=0.043985575n=16S2=0.64538
8、4849S2-S1=0.021653327n=32S2=0.656091436S2-S1=0.010706587n=64S2=0.661402273S2-S1=0.005310836n=128S2=0.664042680S2-S1=0.002640407n=256S2=0.665357576S2-S1=0.001314896n=512S2=0.
此文档下载收益归作者所有