Find the root of given non decreasing function between A and B | Javascript program for the above approach ; Given function ; Function to find the root of the given non - decreasing Function ; To get the minimum possible answer for the root ; Find mid ; Search in [ low , x ] ; Search in [ x , high ] ; Return the required answer ; Function to find the roots of the given equation within range [ a , b ] ; If root doesn 't exists ; Else find the root upto 4 decimal places ; Given range ; Function call
let eps = 1e-6 ; function func ( a , b , c , x ) { return a * x * x + b * x + c ; } function findRoot ( a , b , c , low , high ) { let x = - 1 ; while ( Math . abs ( high - low ) > eps ) { x = ( low + high ) / 2 ; if ( func ( a , b , c , low ) * func ( a , b , c , x ) <= 0 ) { high = x ; } else { low = x ; } } return x ; } function solve ( a , b , c , A , B ) { if ( func ( a , b , c , A ) * func ( a , b , c , B ) > 0 ) { document . write ( " " ) ; } else { document . write ( findRoot ( a , b , c , A , B ) ) ; } } let a = 2 , b = - 3 , c = - 2 , A = 0 , B = 3 ; solve ( a , b , c , A , B ) ;