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)

def squareRootBi(x, epsilon):
    """Assumes x >= 0 and epsilon > 0
    Return 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 = 0
    # bug: when x < 1, sqrt(x) > x = high eg.x=0.25 sqrt(x) = 0.5
    # fix bug: high = max(x, 1.0)
    high = x
    guess = (low + high) / 2.0
    ctr = 1
    while abs(guess ** 2 - x) > epsilon and ctr <= 100:
        # print 'low:', low, 'high:', high, 'guess:', guess
        if guess**2 < x:
            low = guess
        else:
            high = guess
        guess = (low + high) / 2.0
        ctr += 1
    assert ctr <= 100, 'Iteration count exceeded'
    print 'Bi method. Num. iterations:', ctr, 'Estimate:', guess
    return guess

Newton Raphson 牛顿-拉夫森方法

Bisection methods 二分法:

Δ(guess)=guess2x

Δ(guess)=0

Newton 求导

guess点的导数 = guess 点的斜率

Δ(guess)=2guessi=k

k=Δ(guessi)guessi+1guessi

Δ(guessi)=guess2ix

由上面3个式子得到:

guessi+1=guessiΔ(guessi)2guessi

例子:

Δ(3)=3216=7

guessi+1=3Δ(3)2×3=4.1666

def squareRootNR(x, epsilon):
    """Assumes x >= 0 and epsilon > 0
    Return 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)
    x = float(x)
    guess = x / 2.0
    guess = 0.001
    diff = guess ** 2 - x
    ctr = 1
    while abs(diff) > epsilon and ctr <= 100:
        # print 'Error:', diff, 'guess:', guess
        guess = guess - diff/(2.0*guess)
        diff = guess ** 2 - x
        ctr += 1
    assert ctr <= 100, 'Iteration count exceeded'
    print 'NR method. Num. iterations:', ctr, 'Estimate:', guess
    return guess

Lists 列表

3wschool Python 列表

non-scalar types 非基本类型

  • Tuple 元组 (immutable 不可改变的)
  • String 字符串(immutable 不可改变的)
  • List 列表 (mutable 可改变的)
>>> Techs = ['MIT', 'Cal Tech']
>>> print Techs
['MIT', 'Cal Tech']
>>> Ivys = ['Harvard', 'Yale', 'Brown']
>>> print Ivys
['Harvard', 'Yale', 'Brown']
>>> Univs = []
>>> Univs.append(Techs)
>>> print Univs
[['MIT', 'Cal Tech']]
>>> Univs.append(Ivys)
>>> print Univs
[['MIT', 'Cal Tech'], ['Harvard', 'Yale', 'Brown']]
for e in Univs:
    print e
    for c in e: print c

['MIT', 'Cal Tech']
MIT
Cal Tech
['Harvard', 'Yale', 'Brown']
Harvard
Yale
Brown
>>> Univs = Techs + Ivys
>>> print Univs
['MIT', 'Cal Tech', 'Harvard', 'Yale', 'Brown']
>>> Ivys.remove('Harvard')
>>> print Ivys
['Yale', 'Brown']
>>> Ivys[1] = -1
>>> print Ivys
['Yale', -1]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值