0

質問の前に、私は Python を初めて使用し、大学院の特定のクラスで Python を使い始めたばかりだと言いたいと思います。

固定小数点法、二分法、ニュートン ラフソン法などの反復法を使用して関数の根を見つけるスクリプトを作成しました。私のアルゴリズムの python コードは次のとおりです。

固定小数点法:

def fixedpoint(func, g_func,x0,tol, MAXIT):
    
    def g(x):
        return eval(g_func)
    
    print(f"Seeking root of {func}.")
    print("FIXED POINT ITERATION:")
    iterated_x = g(x0)
    for i in range(0, MAXIT):
        iterated_x = g(iterated_x)
        print("Iteration", i + 1, iterated_x)
        condition = abs(g(iterated_x) - (iterated_x)) 
        if condition < tol:
            print(f"The root converges to {iterated_x} after {i+1} iterations.")
            break
        if i == MAXIT and condition >= tol:
            print("ERROR: The root did not converge after maximum iterations.")

fixedpoint('x**2 - 4*x + 2', '(x**2+2)/4', 0.75, 10**(-4), 100)
fixedpoint('math.exp(-x) + x - 7 ', '7 - math.exp(-x)', 0.75, 10**(-4), 100)

二等分法:

def bisection(func,a, b, tol, MAXIT):
    
    def f(x):
        return eval(func)
    
    print(f"Seeking root of {func}")
    print("BISECTION METHOD:")
    if f(a)*f(b) <0:
        for i in range(0, MAXIT):
            c = (a+b)/2
            if f(a)*f(c) < 0:
                a = a
                b = c
                print(f"Iteration", i + 1, f(c))
            elif f(b)*f(c) < 0:
                b = b
                a = c
                print(f"Iteration", i + 1, f(c))
            if f(c) == 0 or abs(f(c)) < tol:
                print ("Exact Solution Found")
                print(f"Iteration", i + 1, c)
                print("The root converges to", c, "after", i + 1, "iterations.")
                break
            elif i == MAXIT and abs(f(c)) > tol:
                print("Bisection method fails.")
                
    return None

bisection('x**2 - 4*x + 2',0.5, 1, 10**(-4), 100)
bisection('math.exp(-x) + x - 7',6, 8, 10**(-4), 100)

ニュートン・ラフソン法:

def newtonraphson(func, deriv, x0, tol, MAXIT):
    
    def f(x):
        return eval(func)

    def ddx(x):
        return eval(deriv)
        
    
    print(f"Seeking root of {func}.")
    print("NEWTON-RAPHSON METHOD:")
    
    for i in range(1, MAXIT):
        iterated_x = x0 - (f(x0)/ddx(x0))
        x0 = iterated_x
        print(f"Iteration", i, x0)

        if f(x0) < tol:
            print(f"The root converges to {x0} after {i} iterations.")
            break
        elif i==MAXIT and f(x0) > tol:
            print("After maximum iterations, root was not found.")

newtonraphson('x**2-4*x+2', '2*x-4', 0.75,10**(-4), 100)
newtonraphson('math.exp(-x) + x - 7', '-(math.exp(-x)) + 1', 0.75,10**(-4), 100)

私のスクリプトは、関心のある方程式の根を見つけることができましたが、より単純な問題で立ち往生しています。基本的に、最大の反復後に許容条件が満たされない場合は、「メソッドが失敗しました」と出力するようにプログラムに伝えたいと思います。

ただし、最大反復回数が 100 に設定され、許容範囲が 0.0001 に設定されている場合に対象外の関数を試してみると、私のコードは必要なステートメントを出力しません。

私の構文は、fail print ステートメントに対して正しいですか?

「if i==MAXIT and f(x0) > tol」のような条件は、私が書いた関数のコンテキストで意味がありますか?

Python の改善に懸命に取り組んでいるため、この問題に関するあらゆるアドバイスをいただければ幸いです。

ありがとうございました。

4

2 に答える 2