MIT公开课: Python 笔记6 二分法,牛顿

Lecture5: Bisection methods , Newton/Raphson, introduction to lists二分法,牛顿,拉复生方法,列表Bisection methods 二分法

注意: # bug: when x < 1, sqrt(x) > x = high eg.x=0.25 sqrt(x) = 0.5 # fix bug: high = max(x, 1.0)

:”””Assumes x >= 0 and epsilon > 0Return y s.t. y*y is within epsilon of x”””assert x >= 0, ‘x must be non-negative, not’ + str(x)assert epsilon > 0, ‘epsilon must be positive, not’ + str(epsilon)low = high = xguess = (low + high) / 2.0ctr = 1while abs(guess ** 2 – x) > epsilon and ctr <= 100:# print ‘low:’, low, ‘high:’, high, ‘guess:’, guessif guess**2 < x:low = guesselse:high = guessguess = (low + high) / 2.0ctr += , ctr, ‘Estimate:’, guessreturn guessNewton Raphson 牛顿-拉夫森方法

Bisection methods 二分法:

,接受自己的失败面,是一种成熟,更是一种睿智;

MIT公开课: Python 笔记6 二分法,牛顿

相关文章:

你感兴趣的文章:

标签云: