Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. def computeDeriv(poly):
  2.    result = []
  3.    for i in range(len(poly)):
  4.       if i==0:
  5.          poly[i] = 0
  6.       result.append(poly[i] * i)
  7.    result.remove(0)
  8.    if len(result) == 0 :
  9.       result.append(0.0)
  10.    return result
  11. #print computeDeriv([20]) - it works from second exercise
  12.  
  13.    
  14. def evaluatePoly(poly, x):
  15.    index = 0
  16.    for i in range(len(poly)):
  17.        index += poly[i]*x**i
  18.    return index
  19. #print evaluatePoly([-12], 3.7) - it works fom first exercise
  20.  
  21.  
  22.  
  23.  
  24. def povtor(poly, x_0) :
  25.    
  26.    proizvodnaia = computeDeriv(poly)
  27.    funkcia_x0 =  evaluatePoly(poly, x_0)
  28.    proizvodnaia_x0 = evaluatePoly(proizvodnaia, x_0)
  29.    x_1 = x_0 - (funkcia_x0/proizvodnaia_x0)
  30.    funkcia_x1 = evaluatePoly(poly, x_1)
  31.    x_0 = x_1
  32.    return funkcia_x1
  33.  
  34.  
  35.  
  36. def computeRoot(poly, x_0, epsilon):
  37.    n = 0
  38.    while abs(povtor(poly, x_0)) >= epsilon :
  39.       n += 1  
  40.    return n
  41. print computeRoot([1, 9, 8], -3, .01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement