资源预览内容
第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
第9页 / 共14页
第10页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
数学实验题目4 Newton迭代法摘要 为初始猜测,则由递推关系产生逼近解的迭代序列,这个递推公式就是Newton法。当距较近时,很快收敛于。但当选择不当时,会导致发散。故我们事先规定迭代的最多次数。若超过这个次数,还不收敛,则停止迭代另选初值。前言 利用牛顿迭代法求的根程序设计流程否是否是是定义输入开 始输出迭代失败标志输出输出奇异标志结 束否问题1(1) 程序运行如下:r = NewtSolveOne(fun1_1,pi/4,1e-6,1e-4,10)r = 0.7391(2) 程序运行如下:r = NewtSolveOne(fun1_2,0.6,1e-6,1e-4,10)r = 0.5885问题2(1) 程序运行如下:r = NewtSolveOne(fun2_1,0.5,1e-6,1e-4,10)r = 0.5671(2) 程序运行如下:r = NewtSolveOne(fun2_2,0.5,1e-6,1e-4,20)r = 0.5669问题3(1) 程序运行如下: p = LegendreIter(2)p = 1.0000 0 -0.3333p = LegendreIter(3)p = 1.0000 0 -0.6000 0p = LegendreIter(4)p = 1.0000 0 -0.8571 0 0.0857p = LegendreIter(5)p = 1.0000 0 -1.1111 0 0.2381 0 p = LegendreIter(6)p = 1.0000 0 -1.3636 0 0.4545 0 -0.0216r = roots(p)r = -0.932469514203150 -0.661209386466265 0.932469514203153 0.661209386466264 -0.238619186083197 0.238619186083197用二分法求根为:r = BinSolve(LegendreP6,-1,1,1e-6)r = -0.932470204878826 -0.661212531887755 -0.238620057397959 0.238600127551020 0.661192602040816 0.932467713647959(2) 程序运行如下: p = ChebyshevIter(2)p = 1.0000 0 -0.5000p = ChebyshevIter(3)p = 1.0000 0 -0.7500 0p = ChebyshevIter(4)p = 1.0000 0 -1.0000 0 0.1250p = ChebyshevIter(5)p = 1.0000 0 -1.2500 0 0.3125 0 p = ChebyshevIter(6)p = 1.0000 0 -1.5000 0 0.5625 0 -0.0313r = roots(p)r = -0.965925826289067 -0.707106781186548 0.965925826289068 0.707106781186547 -0.258819045102521 0.258819045102521用二分法求根为:r = BinSolve(ChebyshevT6,-1,1,1e-6)r = -0.965929926658163 -0.707110969387755 -0.258828922193878 0.258818957270408 0.707105986926020 0.965924944196429与下列代码结果基本一致,只是元素顺序稍有不同:j = 0:5;x = cos(2*j+1)*pi/2/(5+1)x = 0.965925826289068 0.707106781186548 0.258819045102521 -0.258819045102521 -0.707106781186547 -0.965925826289068(3) 程序运行如下: p = LaguerreIter(2)p = 1 -4 2p = LaguerreIter(3)p = 1 -9 18 -6p = LaguerreIter(4)p = 1 -16 72 -96 24p = LaguerreIter(5)p =1.0000 -25.0000 200.0000 -600.0000 600.0000 -120.000 p = LaguerreIter(5)p =1.0000 -25.0000 200.0000 -600.0000 600.0000 -120.000r = roots(p)r = 12.640800844275732 7.085810005858891 3.596425771040711 1.413403059106520 0.263560319718141用二分法求根为: r = BinSolve(LaguerreL5,0,13,1e-6)r = 0.263560314567722 1.413403056105789 3.596425765631150 7.085810005360720 12.640800843813590(4) 程序运行如下: p = HermiteIter(2)p = 1.0000 0 -0.5000p = HermiteIter(3)p = 1.0000 0 -1.5000 0p = HermiteIter(4)p = 1.0000 0 -3.0000 0 0.7500p = HermiteIter(5)p = 1.0000 0 -5.0000 0 3.7500 0 p = HermiteIter(6)p = 1.0000 0 -7.5000 0 11.2500 0 -1.8750r = roots(p)r = -2.350604973674487 2.350604973674488 -1.335849074013696 1.335849074013698 -0.436077411927617 0.436077411927616用二分法求根为: r = BinSolve(HermiteH6,-3,3,1e-6)r = -2.350604981792216 -1.335849100229691 -0.436077818578603 0.436077351472816 1.335848983453244 2.350604952598104所用到的函数function r = NewtSolveOne(fun, x0, ftol, dftol, maxit)% NewtSolveOne 用Newton法解方程f(x)=0在x0附近的一个根% Synopsis: r = NewtSolveOne(fun, x0)% r = NewtSolveOne(fun, x0, ftol, dftol)% Input: fun = (string) 需要求根的函数及其导数% x0 = 猜测根,Newton法迭代初始值% ftol = (optional)误差,默认为5e-9% dftol = (optional)导数容忍最小值,小于它表明Newton法失败,默认为5e-9% maxit = (optional)迭代次数,默认为25% Output: r = 在寻根区间内的根或奇点 if nargin 3 ftol = 5e-9; end if nargin 4 dftol = 5e-9; end if nargin 5 maxit = 25; end x = x0; %设置初始迭代位置为x0 k = 0; %初始化迭代次数为0 while k = maxit k = k + 1; f,dfdx = feval(fun,x); %fun返回f(x)和f(x)的值 if abs(dfdx) dftol %如果导数小于dftol,Newton法失败,返回空值 r = ; warning(dfdx is too small!); return; end dx = f/dfdx; %x(n+1) = x(n) - f( x(n) )/f( x(n) ),这里设dx = f( x(n) )/f( x(n) ) x = x - dx; if abs(f) ftol %如果误差小于ftol,返回当前x为根 r = x; return; end end r = ; %如果牛顿法未收敛,返回空值function p = LegendreIter(n)% LegendreIter 用递推的方法计算n次勒让德多项式的系数向量 Pn+2(x) = (2*i+3)/(i+2) * x*Pn+1(x) - (i+1)/(i+2) * Pn(x)% Synopsis: p = LegendreIter(n)% Input: n = 勒让德多项式的次数
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号