Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def evaluate_poly(poly, x):
- valpoly = 0
- for i in range(0, len(poly)):
- valpoly = valpoly + (poly[i]*x**i) ## changed sumpoly to valpoly
- return valpoly ## return the value of poly at x
- def compute_deriv(poly):
- deriv = ()
- for i in range(0, len(poly)):
- deriv = deriv + (poly[i]*i,)
- return deriv ## function returns a tuple representation of the derivative of "poly"
- def compute_root(poly, x_0, epsilon):
- value = evaluate_poly(poly, x_0)
- count = 0
- while (value > epsilon) or (value < -epsilon):
- x_0 = x_0 - ((value)/(evaluate_poly(compute_deriv(poly), x_0)))
- value = evaluate_poly(poly, x_0)
- count += 1
- return (x_0, count)
- polyMy = (-13.39, 0.0, 17.5, 3.0, 1.0)
- print compute_root(polyMy, .01, .0001)
- ## Output should be (0.80679042259850975, 15239)
Advertisement
Add Comment
Please, Sign In to add comment