Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import numpy as np
  2. from math import sqrt
  3. from scipy.misc import derivative
  4.  
  5. def newton_method(x, x1, function):
  6. return x-(function(x)/derivative(function, x))
  7.  
  8.  
  9. def drying_method(x0, x1, function):
  10. return (x0*function(x1)-x1*function(x0))/(function(x1)-function(x0))
  11.  
  12. def bissection(x0,x1,function,precision=1e-3):
  13. if x0 > x1:
  14. x0,x1 = x1,x0
  15. k = 0
  16. mei = (x0+x1)/2.0
  17. while abs(x1-x0)>=precision and abs(function(mei))>=precision:
  18. k += 1
  19. if function(mei)*function(x0)<=0:
  20. x1 = mei
  21. else:
  22. x0 = mei
  23. mei = (x0+x1)/2.0
  24. # print str(mei)
  25. return mei,k
  26.  
  27.  
  28. def fixed_point_template(x0,x1,iteration_function,function,precision=1e-3):
  29. k = 0
  30. x = iteration_function(x0,x1,function)
  31. while abs(x-x1)>=precision and abs(function(x1))>=precision:
  32. k += 1
  33. x0,x1 = x1, x
  34. x = iteration_function(x0,x1,function)
  35. return x,k
  36.  
  37.  
  38.  
  39.  
  40. def chaos_function(x):
  41. return (8 - sqrt(400-x**2)) * sqrt(900-x**2) + 8 * sqrt(400-x**2)
  42. # return ((8/sqrt(400-x**2)) - 1) * sqrt(900-x**2) + 8
  43.  
  44. def carlos_function(x):
  45. return (sqrt(400-x**2)*sqrt(900-x**2))/(sqrt(400-x**2)+sqrt(900-x**2))-8
  46.  
  47.  
  48.  
  49. # x,n = bissection(1.0,20.0,chaos_function)
  50. x0,x1 = 8,20
  51. # try:
  52. #
  53. # except Exception:
  54. # print "Errou"
  55.  
  56. x,n = fixed_point_template(x0,x1,drying_method,chaos_function)
  57. print "Chaos:raiz:"+str(x)+", iterations:"+str(n)
  58.  
  59. # x,n = fixed_point_template(x0,x1,newton_method,carlos_function)
  60. # print "Carlo:raiz:"+str(x)+", iterations:"+str(n)
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement