Python lookup function f (x) = 0 Root Solution

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.