szymcio10

bisekcja

Mar 15th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. import math
  2. def bisection(f,a,b,N):
  3.     if f(a)*f(b) >= 0:
  4.         print("Błąd")
  5.         return None
  6.     a_n = a
  7.     b_n = b
  8.     for n in range(1,N+1):
  9.         m_n = (a_n + b_n)/2
  10.         f_m_n = f(m_n)
  11.         if f(a_n)*f_m_n < 0:
  12.             a_n = a_n
  13.             b_n = m_n
  14.         elif f(b_n)*f_m_n < 0:
  15.             a_n = m_n
  16.             b_n = b_n
  17.         elif f_m_n == 0:
  18.             print("Nie znaleziono rozwiazania")
  19.             return m_n
  20.         else:
  21.             print("Błąd")
  22.             return None
  23.     return (a_n + b_n)/2
  24.  
  25. print(bisection(math.sin,-1.5,1,25))
Advertisement
Add Comment
Please, Sign In to add comment