资源描述:
《用Romberg方法求解积分.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1.用Romberg方法求解积分,要求误差不超过解:Romberg.m文件:function[I,step]=Romberg(f,a,b,EPS)%Romberg.m是用龙贝格公式求积分%f为被积函数%EPS为积分结果精度%a,b为积分区间的上下限%I为积分结果;step为积分的子区间数m=1k=0Er=0.1H=b-aS=zeros(1,1)S(1,1)=(H/2)*(subs(sym(f),findsym(sym(f)),a)+subs(sym(f),findsym(sym(f)),b))whileEr>EP
2、Sk=k+1f1=0H=H/2fori=1:mx=a+H*(2*i-1)f1=f1+subs(sym(f),findsym(sym(f)),x)endS(k+1,1)=S(k,1)/2+H*f1m=2*mforn=1:kS(k+1,n+1)=S(k+1,n)+(S(k+1,n)-S(k,n))/(4^n-1)endEr=abs(S(k+1,n+1)-S(k,n))endI=S(k+1,k+1)step=k命令:clearclcformatshorta=0;b=0.8;EPS=1e-2;[I,step]=Rombe
3、rg('x^(1/2)',a,b,EPS)计算结果:m=1k=0Er=0.1000H=0.8000S=0S=0.3578k=1f1=0H=0.4000x=0.4000f1=0.6325S=0.35780.4319m=2S=0.357800.43190.4566Er=0.0988k=2f1=0H=0.2000x=0.2000f1=0.4472x=0.6000f1=1.2218S=0.357800.43190.45660.46030m=4S=0.357800.43190.45660.46030.4698S=0.357
4、8000.43190.456600.46030.46980.4707Er=0.0141k=3f1=0H=0.1000x=0.1000f1=0.3162x=0.3000f1=0.8640x=0.5000f1=1.5711x=0.7000f1=2.4077S=0.3578000.43190.456600.46030.46980.47070.470900m=8S=0.3578000.43190.456600.46030.46980.47070.47090.47450S=0.3578000.43190.456600.46
5、030.46980.47070.47090.47450.4748S=0.35780000.43190.4566000.46030.46980.470700.47090.47450.47480.4748Er=0.0042I=0.4748step=3I=0.4748step=31.设方程组试用Jacobi迭代法求解此方程,,当时终止迭代。解:Jacobi.m文件:functionJacobi(A,b,max,eps)%max为最大迭代次数,eps为容许误差n=length(A);x=zeros(n,1);x1=zer
6、os(n,1);k=0;while1x1(1)=(b(1)-A(1,2:n)*x(2:n,1))/A(1,1)fori=2:n-1x1(i)=(b(i)-A(i,1:i-1)*x(1:i-1,1)-A(i,i+1:n)*x(i+1:n,1))/A(i,i)endx1(n)=(b(n)-A(n,1:n-1)*x(1:n-1,1))/A(n,n)k=k+1ifsum(abs(x1-x))=maxfprintf('TheMethodisdi
7、sconvergent')breakendx=x1endifk8、1000x0=000B=0-0.4000-0.20000.25000-0.5000-0.20000.30000f=-2.40005.00000.3000x=-2.40005.00000.3000x0=-2.40005.00000.3000i=2x=-4.46004.25002.2800x0=-4.46004.25002.2800i=3x=-4.55602.74502.46