#NEWLINE# def msolve( f , x ) : #NEWLINE# #INDENT# '\n    Finds the first root of f(x) to the left of 0.\n\n    The x0 and dx below are taylored to get the correct result for our\n    particular function --- the general solver often overshoots the first\n    solution.\n    ' #NEWLINE# f = lambdify ( x , f ) #NEWLINE# x0 = ( - 0.001 ) #NEWLINE# dx = 0.001 #NEWLINE# while ( ( f ( ( x0 - dx ) ) * f ( x0 ) ) > 0 ) : #NEWLINE# #INDENT# x0 = ( x0 - dx ) #NEWLINE# #UNINDENT# x_max = ( x0 - dx ) #NEWLINE# x_min = x0 #NEWLINE# assert ( f ( x_max ) > 0 ) #NEWLINE# assert ( f ( x_min ) < 0 ) #NEWLINE# for n in range ( 100 ) : #NEWLINE# #INDENT# x0 = ( ( x_max + x_min ) / 2 ) #NEWLINE# if ( f ( x0 ) > 0 ) : #NEWLINE# #INDENT# x_max = x0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# x_min = x0 #NEWLINE# #UNINDENT# #UNINDENT# return x0