牛顿、拉复生方法求开方,牛顿拉复生开方,[Python]代码de


[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

牛顿、拉复生思想求开方

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

评论关闭