Advertisement
Guest User

Untitled

a guest
Sep 30th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. poly = (0.0, 0.0, 5.0, 9.3, 7.0)
  2. x = -13
  3.  
  4. def evaluate_poly(poly, x):
  5.     """
  6.    Computes the polynomial function for a given value x. Returns that value.
  7.  
  8.    Example:
  9.    >>> poly = (0.0, 0.0, 5.0, 9.3, 7.0)    # f(x) = 7x^4 + 9.3x^3 + 5x^2
  10.    >>> x = -13
  11.    >>> print evaluate_poly(poly, x)  # f(-13) = 7(-13)^4 + 9.3(-13)^3 + 5(-13)^2
  12.    180339.9
  13.  
  14.    poly: tuple of numbers, length > 0
  15.    x: number
  16.    returns: float
  17.    """
  18.     # TO DO ...
  19.     answer = 0
  20.     for power in range (0, len(poly)):
  21.         answer = answer + poly[power]*(x**power)
  22.     return answer
  23.    
  24. print evaluate_poly(poly, x)
  25.            
  26. poly1 = (-13.39, 0.0, 17.5, 3.0, 1.0)
  27. # derivative would be (0, 35, 9, 4)
  28.  
  29. def compute_deriv(poly1):
  30.     """
  31.    Computes and returns the derivative of a polynomial function. If the
  32.    derivative is 0, returns (0.0,).
  33.  
  34.    Example:
  35.    >>> poly = (-13.39, 0.0, 17.5, 3.0, 1.0)    # x^4 + 3x^3 + 17.5x^2 - 13.39
  36.    >>> print compute_deriv(poly)        # 4x^3 + 9x^2 + 35^x
  37.    (0.0, 35.0, 9.0, 4.0)
  38.  
  39.    poly: tuple of numbers, length > 0
  40.    returns: tuple of numbers
  41.    """
  42.     # TO DO ...
  43.     answer = ()
  44.     for a in range (1, len(poly1)):
  45.         answer = answer + (a*poly1[a],)
  46.     return answer
  47.    
  48. print compute_deriv(poly1)
  49.        
  50.        
  51. x_0 = .1
  52. poly = (-13.39, 0.0, 17.5, 3.0, 1.0)
  53. epsilon = .0001
  54.  
  55. def compute_root(poly, x_0, epsilon):
  56.     """
  57.    Uses Newton's method to find and return a root of a polynomial function.
  58.    Returns a tuple containing the root and the number of iterations required
  59.    to get to the root.
  60.  
  61.    Example:
  62.    >>> poly = (-13.39, 0.0, 17.5, 3.0, 1.0)    #x^4 + 3x^3 + 17.5x^2 - 13.39
  63.    >>> x_0 = 0.1
  64.    >>> epsilon = .0001
  65.    >>> print compute_root(poly, x_0, epsilon)
  66.    (0.80679075379635201, 8.0)
  67.  
  68.    poly: tuple of numbers, length > 1.
  69.         Represents a polynomial function containing at least one real root.
  70.         The derivative of this polynomial function at x_0 is not 0.
  71.    x_0: float
  72.    epsilon: float > 0
  73.    returns: tuple (float, int)
  74.    """
  75.     # TO DO ...
  76.     guesses = 1
  77.     while abs(evaluate_poly(poly, x_0)) >= epsilon:  
  78.         x_0 = x_0 - evaluate_poly(poly, x_0)/evaluate_poly(compute_deriv(poly), x_0)
  79.         guesses = guesses +1   
  80.     if abs(evaluate_poly(poly, x_0)) <= epsilon:
  81.         return "The root of the polynomial is", (x_0,guesses)
  82.     else:
  83.         return "The polynomial has no root"
  84.        
  85. print compute_root(poly, x_0, epsilon)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement