Advertisement
Guest User

bisekcja

a guest
Dec 13th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. def bisekcja(a0, b0, eps, alfa):
  2.     a = []
  3.     b = []
  4.     s = []
  5.     a.append(a0)
  6.     b.append(b0)
  7.     while abs(a[-1]-b[-1]) > eps and len(s) < 10000:
  8.         #s.append((a[-1]+b[-1])/2)
  9.         s.append(a[-1] +((b[-1]-a[-1])*alfa))
  10.         if f(s[-1])*f(a[-1]) < 0:
  11.             a.append(a[-1])
  12.             b.append(s[-1])
  13.         else:
  14.             a.append(s[-1])
  15.             b.append(b[-1])
  16.     plt.figure(1, figsize=(8,8))
  17.     plt.plot(a)
  18.     plt.plot(b)
  19.     plt.figure(2, figsize=(8,8))
  20.     x = np.linspace(a0,b0, 30)
  21.     plt.plot(x,f(x))
  22.     plt.scatter(s,f(s), c=range(len(s)), cmap=plt.get_cmap('Reds'))
  23.     plt.xlim([a0, b0])
  24.     print(len(s))
  25.     return s[-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement