Program for Method Of False Position | Python3 implementation of Bisection Method for solving equations ; An example function whose solution is determined using Bisection Method . The function is x ^ 3 - x ^ 2 + 2 ; Prints root of func ( x ) in interval [ a , b ] ; Initialize result ; Find the point that touches x axis ; Check if the above found point is root ; Decide the side to repeat the steps ; Initial values assumed
MAX_ITER = 1000000 NEW_LINE def func ( x ) : NEW_LINE INDENT return ( x * x * x - x * x + 2 ) NEW_LINE DEDENT def regulaFalsi ( a , b ) : NEW_LINE INDENT if func ( a ) * func ( b ) >= 0 : NEW_LINE INDENT print ( " You ▁ have ▁ not ▁ assumed ▁ right ▁ a ▁ and ▁ b " ) NEW_LINE return - 1 NEW_LINE DEDENT c = a NEW_LINE for i in range ( MAX_ITER ) : NEW_LINE INDENT c = ( a * func ( b ) - b * func ( a ) ) / ( func ( b ) - func ( a ) ) NEW_LINE if func ( c ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT elif func ( c ) * func ( a ) < 0 : NEW_LINE INDENT b = c NEW_LINE DEDENT else : NEW_LINE INDENT a = c NEW_LINE DEDENT DEDENT print ( " The ▁ value ▁ of ▁ root ▁ is ▁ : ▁ " , ' % .4f ' % c ) NEW_LINE DEDENT a = - 200 NEW_LINE b = 300 NEW_LINE regulaFalsi ( a , b ) NEW_LINE