1、计算材料学考试分别用前向2点后向2点,对称3点对称5点方法计算f(x=lnx在x=2处的一阶导数和误差,给出不同 h 值(0.5, 0.2, 0.1, 0.05, 0.02, 0.01, 0.005, 0.002,0.001,0.0005, 0.0002, 0.000 仆各方法的误差1.1对称三点法program lximplicit nonedoubleprecisio n,parameter : x=2.0d0 doubleprecisi on : exact,h,f_sym3,diff_sym3h=0.5d0exact=x*(-1do while (h0write(*,* En ter
2、value of h (=0 to stopread(*,* hif (h=0.0 thenstop h0write(*,* Enter value of h (=0 to stopread(*,* hif (h=0.0 thenstop h0write(*,* Enter value of h (=0 to stopread(*,* hif (h=0.0 thenstop h0write(*,* Enter value of h (=0 to stopread(*,* hif (h=0.0d0 thenstop h 0write(*,* Enter value of N (eve n,=2
3、read(*,* nif(n2 thenstop n 0write(*,* Enter value of N (eve n,=2 read(*,* nif(n2 thenstop n 0write(*,* Enter value of N (eve n,=2 read(*,* nif(n2 thenstop n 0write(*,* Enter value of N (eve n,=2 read(*,* nif(n2 thenstop n tolxiter=ite r+1x=x+dxif(fold*fu nc(x0.0 the ncycleelsex=x-dxdx=dx/2en difwrit
4、e(*,(l4,6f12.8 iter,x,exact-x enddoend program root5.2牛顿法doubleprecisi on function func(ximplicit nonedoubleprecisi on : xfun c=x*2-5endfunction funcdoubleprecisi on function fun c_prime (ximplicit nonedoubleprecisi on : xfun c_prime=2*xendfunction fun c_primeprogram rootimplicit nonein teger : iter
5、=0doubleprecision : x,xO,dx,guess_st,tolx,exact,func,func_primeexact=sqrt(5.0d0guess_st=1.OdOdx=0.5tolx=1.0d-8 x=guess_st do while(abs(dx tolx iter=ite r+1x0=xx=x0-f un c(x0/f un c_prime(x0 dx=x-x0write(*,(l4,6f12.8 iter,x,exact-x en ddoend program root5.3弦割法doubleprecisi on function func(x implicit
6、 nonedoubleprecisi on : xfun c=x*2-5endfunction funcprogram rootimplicit nonein teger : iter=0doubleprecision : x,x0,dx,guess_st,x3_st,xold,tolx,exact,funcexact=sqrt(5.0d0guess_st=1.0d0dx=0.5tolx=1.0d-8x=guess_stdo while(abs(dx tolxiter=ite r+1xold=x0x0=xif(iter=1 the nx=x3_stelsex=x0-func(x0*(x0-xold/(func(x0-func(xold endifdx=x-x0write(*,(l4,6f12.8 iter,x,exact-x enddoend program root
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1