玩蛇网提供最新Python编程技术信息以及Python资源下载!

python方法求开方牛顿、拉复生方法

python 培训

Python求开方牛顿、拉复生方法

def SquarerootNR(x,eplison):
    assert x>=0, 'x must be non negtive not'+str(x)
    assert eplison>0,'eplison must be positive not'+str(eplison)
    x=float(x)
    guess=x
    diff=guess**2-x
    ctr=1
    while abs(diff)>eplison and ctr<=100:
        guess=guess-diff/(2*guess)
        diff=guess**2-x
        ctr+=1
    assert ctr<=100 ,'the times of iteration is too much'
    print 'NR method:'
    print 'guess: %f iteration: %d' %(guess,ctr)
    return guess

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/algorithm/a2401.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-13 14:27 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部