2

ニュートン法を解くためにこのコードを手に入れました。しかし、ゼロ除算エラーが発生します。何が悪いのかわかりません。ありがとうございました。

import copy

tlist = [0.0, 0.12, 0.16, 0.2, 0.31, 0.34] # list of start time for the phonemes

w = w1 = w2 = w3 = w = 5

def time() :
    frame = 0.04
    for i, start_time in enumerate(tlist) :
        end_time = tlist[i]
        frame = frame * (i + 1)
        poly = poly_coeff(start_time, end_time, frame)
        Newton(poly) 

def poly_coeff(stime, etime, f) :
    """The equation is k6 * u^3 + k5 * u^2 + k4 * u + k0 = 0. Computing the coefficients for this polynomial."""
    """Substituting the required values we get the coefficients."""
    t_u = f
    t0 = stime
    t3 = etime
    t1 = t2 = (stime + etime) / 2
    w0 = w1 = w2 = w3 = w
    k0 = w0 * (t_u - t0)
    k1 = w1 * (t_u - t1)
    k2 = w2 * (t_u - t2)
    k3 = w3 * (t_u - t3)
    k4 = 3 * (k1 - k0)
    k5 = 3 * (k2 - 2 * k1 + k0)
    k6 = k3 - 3 * k2 + 3 * k1 -k0 

    return [[k6,3], [k5,2], [k4,1], [k0,0]]

def poly_differentiate(poly):
    """ Differentiate polynomial. """
    newlist = copy.deepcopy(poly)

    for term in newlist:
        term[0] *= term[1]
        term[1] -= 1

    return newlist

def poly_substitute(poly, x):
    """ Apply value to polynomial. """
    sum = 0.0 

    for term in poly:
        sum += term[0] * (x ** term[1])
    return sum

def Newton(poly):
    """ Returns a root of the polynomial"""
    poly_diff = poly_differentiate(poly) 
    counter = 0
    epsilon = 0.000000000001

    x = float(raw_input("Enter initial guess:"))

    while True:
        x_n = x - (float(poly_substitute(poly, x)) / poly_substitute(poly_diff, x))
        counter += 1
        if abs(x_n - x) < epsilon :
            break
        x = x_n
    print "Number of iterations:", counter
    print "The actual root is:", x_n
    return x_n

if __name__ == "__main__" :
    time()
Enter initial guess:0.5
Traceback (most recent call last):
  File "newton.py", line 79, in <module>
    time()
  File "newton.py", line 18, in time
    Newton(poly) 
  File "newton.py", line 67, in Newton
    x_n = x - (float(poly_substitute(poly, x)) / poly_substitute(poly_diff, x))
ZeroDivisionError: float division
4

3 に答える 3

6

ここに基本的なバグがあります:

for i, start_time in enumerate(tlist):
    end_time = tlist[i]

の性質上enumeratestart_timeend_timeは同じ値になります。これは、毎回poly_coeff返されることを意味します。[[0,3], [0,2], [0,1], [0,0]]この結果が に (経由でNewton)渡されるpoly_differentiateと、結果は になります[[0,2], [0,1], [0,0], [0,-1]]

に渡されたこの結果は、すべてのリスト エントリを合計する前に (たまたまゼロになる) でpoly_substitute乗算するため、ゼロの合計になります。term[0]次に、 - をゼロで割ります。

解決策(コメントごとに編集)

正しい値start_timeend_time値を使用してください。ご希望のようですend_time = tlist[i+1]。これのエッジ条件は、最終的なリスト エントリを評価せずに抜け出すことです。あなたが本当に欲しいのはこれです:

for i, start_time in enumerate(tlist[:-1]):
    end_time = tlist[i+1]
于 2011-08-12T14:52:16.360 に答える
3

あなたのコードをコピーして、少しデバッグしようとしました。

一般に、コードがゼロ値を返し、除算中にそれを使用しようとしたためです。

コードを注意深く確認すると、次のループが見つかります。

for i, start_time in enumerate(tlist) :
        end_time = tlist[i]

最初の繰り返しで start_time == 0.0 と endTime == 0.0 が得られます。

これにより、次の行が導かれます。

poly = poly_coeff(start_time, end_time, frame)

返品するには:

>>> [[0.0, 3], [0.0, 2], [0.0, 1], [0.2, 0]]

この原因:

poly_substitute(poly_diff, x)

次のループを使用している場所:

for term in poly:
    sum += term[0] * (x ** term[1])

ゼロのみを乗算しているため、ゼロを返します。

したがって、0で削除しようとすると、言及された例外が発生します。

これは、安全にチェックして endTime を tList[i+1] に設定するようにコードを変更すると、このエラーが解消されることを意味します - 'i+1 をチェックすることを忘れないでください

于 2011-08-12T14:51:51.787 に答える
0

一目で

 poly_substitue(poly_diff,x) 

特別な x ではゼロのようです。各更新の前に x を出力して、反復を追跡してみてください。

しかし、例外はコードのバグによって引き起こされると思います。多項式の絶対係数 C*X^0 は 0 * X^-1 に微分されるため、x=0poly_substituteのときに a が発生します。ZeroDivisionException

于 2011-08-12T14:49:41.220 に答える