Advertisement
Guest User

Untitled

a guest
May 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. # Creator _Sabyasachi
  2.  
  3. import numpy as np
  4. import matplotlib.pyplot as plot
  5.  
  6. total_iteration = 1
  7. number_of_iteration = 1
  8.  
  9.  
  10. def plotting(z):
  11. to_return = (z / (1 - z)) * (np.sqrt(6 / (2 + z))) - .05
  12. return to_return
  13.  
  14.  
  15. def graph(k):
  16. y = plotting(k)
  17. plot.figure(figsize=(5, 5))
  18. plot.plot(k, y)
  19. plot.grid()
  20. plot.show()
  21.  
  22.  
  23. def secant_method(func, first_guess, second_guess, relative_error, max_iteration):
  24. global total_iteration
  25. p = 0.0
  26. if func(first_guess) * func(second_guess) > 0:
  27. return None
  28.  
  29. while total_iteration < max_iteration and np.abs(second_guess - first_guess) > relative_error:
  30. try:
  31. a = first_guess
  32. b = second_guess
  33. p = (a*func(b) - b*func(a))/(func(b) - func(a))
  34. except ZeroDivisionError:
  35. print("Error! - Divided by zero")
  36. exit(-1)
  37. first_guess = second_guess
  38. second_guess = p
  39. total_iteration += 1
  40. if abs(func(second_guess)) > relative_error:
  41. print("Secant fails after ", total_iteration, " number of iteration")
  42. total_iteration = -1
  43. return p
  44.  
  45.  
  46. def false_position_method(func, lower_bracket, upper_bracket, app_error, max_iteration):
  47. global number_of_iteration
  48. fa = func(lower_bracket)
  49.  
  50. while number_of_iteration <= max_iteration:
  51. a = lower_bracket
  52. b = upper_bracket
  53. p = (a * func(b) - b * func(a)) / (func(b) - func(a))
  54. fp = func(p)
  55.  
  56. if fp == 0 or np.abs(func(p)) <= app_error:
  57. return p
  58. elif fp * fa >= 0:
  59. fa = fp
  60. lower_bracket = p
  61. else:
  62. upper_bracket = p
  63. number_of_iteration += 1
  64.  
  65. return None
  66.  
  67.  
  68. x = np.arange(-1.9, 1, 0.1)
  69. graph(x)
  70. tolerable_error = 5e-3
  71.  
  72. iteration_counter = int(input("Enter the maximum number of iteration for Secant and False Position Method : "))
  73. initial_guess = float(input("Enter 1st initial guess for Secant Method : "))
  74. initial_guess_2 = float(input("Enter 2nd initial guess for Secant Method : "))
  75. lower_bound = float(input("Enter lower bound of the bracket for False Position Method : "))
  76. upper_bound = float(input("Enter upper bound of the bracket for False Position Method : "))
  77.  
  78. sect_method = secant_method(plotting, initial_guess, initial_guess_2, tolerable_error, iteration_counter)
  79.  
  80. print()
  81. print("Showing the result for Secant Method :")
  82. if total_iteration > 0:
  83. print("Result found between the given interval ")
  84. print("Result :", sect_method, " iteration:", total_iteration)
  85. else:
  86. print("No result found between the given interval :'( \nGood Luck!")
  87.  
  88. fpm = false_position_method(plotting, lower_bound, upper_bound, tolerable_error, iteration_counter)
  89. print("Showing the result for FP Method :")
  90.  
  91. if number_of_iteration > 0:
  92. print("Result found between the given interval ")
  93. print("Result :", fpm, " iteration:", number_of_iteration)
  94. else:
  95. print("No result found between the given interval :'( \nGood Luck!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement