MIT 6.00.1X --Week 3

来源:互联网 发布:win10系统优化固态 编辑:程序博客网 时间:2024/06/09 20:03

NEWTON-RAPHSON ROOT FINDING


  • General approximation algorithm to find roots of a
    polynomial in one variable

p(x)=anxn+an1xn1++a1x+a0

  • Want to find r such that p(r) = 0
  • Newton showed that if g is an approximation to the
    root, then
    gp(g)/p(g)
    is a better approximation; where p’ is derivative of p.
# Lecture 3.7, slide 3# Newton-Raphson for square rootepsilon = 0.01y = 24.0guess = y/2.0while abs(guess*guess - y) >= epsilon:    guess = guess - (((guess**2) - y)/(2*guess))    print(guess)print('Square root of ' + str(y) + ' is about ' + str(guess))

# lecture 3.6, slide 2# bisection search for square rootx = 12345epsilon = 0.01numGuesses = 0low = 0.0high = xans = (high + low)/2.0while abs(ans**2 - x) >= epsilon:    print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))    numGuesses += 1    if ans**2 < x:        low = ans    else:        high = ans    ans = (high + low)/2.0print('numGuesses = ' + str(numGuesses))print(str(ans) + ' is close to square root of ' + str(x))

普通迭代

x = 25epsilon = 0.01step = 0.1guess = 0.0while guess <= x:    if abs(guess**2 -x) < epsilon:        break    else:        guess += stepif abs(guess**2 - x) >= epsilon:    print 'failed'else:    print 'succeeded: ' + str(guess)

FLOATING POINT ACCURACY


浮点数的二进制表示与还原

先挖个坑。。。
参考《计算机专业导论之思维与系统》

原创粉丝点击