Advertisement
Kitood

Untitled

Feb 10th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. # BISECTION METHOD
  2. # Aim - TO IMPLEMENT BISECTION METHOD USING JUPYTER NOTEBOOK
  3. from math import log, ceil
  4. x=0
  5.  
  6. def bisec(x):
  7. func = (x**3)-(4*x)-9
  8. return func
  9.  
  10. a=2
  11. b=3
  12. err = 0.02
  13. c1=(a+b)/2
  14. print(c1)
  15. c2=0
  16. n= (log(b-a)-log(err))/log(2)
  17. print(n)
  18. i=0
  19. while i<ceil(n):
  20. # print("c1 b4= ",c1)
  21. # print("c2 b4= ",c2)
  22. # print("a b4 = ",a)
  23. # print("b b4 = ",b)
  24. if bisec(b)*bisec(c1)<0:
  25. c2=c1
  26. a=c1
  27. print("a becomes c")
  28.  
  29. elif bisec(a)*bisec(c1)<0:
  30. c2=c1
  31. b=c1
  32. print("b becomes a")
  33. print("c1 af= ",c1)
  34. # print("c2 af= ",c2)
  35. # print("a af = ",a)
  36. # print("b af = ",b)
  37. print("i = ",i)
  38. c1=(a+b)/2
  39. i+=1
  40. print("Root is - ",c1)
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement