Program for Muller Method | Python3 Program to find root of a function , f ( x ) ; Function to calculate f ( x ) ; Taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; Calculating various constants required to calculate x3 ; Taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; Driver Code
import math ; NEW_LINE MAX_ITERATIONS = 10000 ; NEW_LINE def f ( x ) : NEW_LINE INDENT return ( 1 * pow ( x , 3 ) + 2 * x * x + 10 * x - 20 ) ; NEW_LINE DEDENT def Muller ( a , b , c ) : NEW_LINE INDENT res = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT f1 = f ( a ) ; f2 = f ( b ) ; f3 = f ( c ) ; NEW_LINE d1 = f1 - f3 ; NEW_LINE d2 = f2 - f3 ; NEW_LINE h1 = a - c ; NEW_LINE h2 = b - c ; NEW_LINE a0 = f3 ; NEW_LINE a1 = ( ( ( d2 * pow ( h1 , 2 ) ) - ( d1 * pow ( h2 , 2 ) ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; NEW_LINE a2 = ( ( ( d1 * h2 ) - ( d2 * h1 ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; NEW_LINE x = ( ( - 2 * a0 ) / ( a1 + abs ( math . sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; NEW_LINE y = ( ( - 2 * a0 ) / ( a1 - abs ( math . sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; NEW_LINE if ( x >= y ) : NEW_LINE INDENT res = x + c ; NEW_LINE DEDENT else : NEW_LINE INDENT res = y + c ; NEW_LINE DEDENT m = res * 100 ; NEW_LINE n = c * 100 ; NEW_LINE m = math . floor ( m ) ; NEW_LINE n = math . floor ( n ) ; NEW_LINE if ( m == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT a = b ; NEW_LINE b = c ; NEW_LINE c = res ; NEW_LINE if ( i > MAX_ITERATIONS ) : NEW_LINE INDENT print ( " Root ▁ cannot ▁ be ▁ found ▁ using " , " Muller ' s ▁ method " ) ; NEW_LINE break ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT if ( i <= MAX_ITERATIONS ) : NEW_LINE INDENT print ( " The ▁ value ▁ of ▁ the ▁ root ▁ is " , round ( res , 4 ) ) ; NEW_LINE DEDENT DEDENT a = 0 ; NEW_LINE b = 1 ; NEW_LINE c = 2 ; NEW_LINE Muller ( a , b , c ) ; NEW_LINE