Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. # Secant Method : algorithm for finding roots (3000 years older than Netwons Method)
  2. # ragulbalaji19
  3.  
  4. import math
  5.  
  6. def f(x): return math.sin(x) - x**2 # function we want to study
  7.  
  8. pts = [0.75, 0.8]
  9. n = 2
  10. print(n, pts)
  11.  
  12. for i in range(200):
  13. try:
  14. pts.append(pts[n-1] - f(pts[n-1]) * (pts[n-1]-pts[n-2]) / (f(pts[n-1])-f(pts[n-2])))
  15. print(n, pts[n])
  16. if abs(pts[n] - pts[n-1]) < 0.000000001: # 9dp precision
  17. print("Converged.")
  18. break
  19. n+=1
  20. except Exception as e:
  21. print('halted', e)
  22. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement