This example describes the workaround for Python lookup function f (x) = 0 root. Share to everyone for your reference. The implementation method is as follows:
"root = Ridder (f,a,b,tol=1.0e-9). Finds a root of f (x) = 0 with Ridder ' s method. The root must is bracketed in (A, b). " Import errorfrom Math Import Sqrtdef Ridder (f,a,b,tol=1.0e-9): FA = f (a) if fa = = 0.0:return a fb = f (b) If fb = = 0.0:return b if FA*FB > 0.0:error.err (' Root is not bracketed ') for I in range: # Compute the Improved root x from Ridder ' s formula C = 0.5* (A + b); FC = f (c) s = sqrt (FC**2-FA*FB) if s = = 0.0:retur n None dx = (c-a) *fc/s if (FA-FB) < 0.0:DX =-dx x = c + dx; FX = f (x) # Test for convergence< C16/>if i > 0: if ABS (X-xold) < Tol*max (ABS (x), 1.0): return x xold = x # Re-bracket the root as Tig htly as possible if Fc*fx > 0.0: if fa*fx < 0.0:b = x; fb = FX else: a = x; fa = FX else:< C25/>a = C; b = x; FA = FC; FB = FX return None print ' Too many iterations '
Hopefully this article will help you with Python programming.