Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Secant Method : algorithm for finding roots (3000 years older than Netwons Method)
- # ragulbalaji19
- import math
- def f(x): return math.sin(x) - x**2 # function we want to study
- pts = [0.75, 0.8]
- n = 2
- print(n, pts)
- for i in range(200):
- try:
- pts.append(pts[n-1] - f(pts[n-1]) * (pts[n-1]-pts[n-2]) / (f(pts[n-1])-f(pts[n-2])))
- print(n, pts[n])
- if abs(pts[n] - pts[n-1]) < 0.000000001: # 9dp precision
- print("Converged.")
- break
- n+=1
- except Exception as e:
- print('halted', e)
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement