DiQ

goldenSectionSearch

DiQ
Jan 26th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. from math import *
  2. def func(var):
  3.     value = pow((var-5),2) + 3
  4.     return value
  5.  
  6. k = (sqrt(5)+1)/2
  7. a = int(input('podaj wartosc a: '))
  8. b = int(input('podaj wartosc b: '))
  9. if(a>b):
  10.     print('a ma byc mniejsze od b, wiec zostaly zamienione miejscami')
  11.     c = a
  12.     a = b
  13.     b = c
  14. xL = b - k * (b - a)
  15. xR = a + k * (b - a)
  16. epsi = pow(10, -3)
  17. while (abs((b - a)) > epsi):
  18.     if func(xL)<func(xR):
  19.         b = xR
  20.         xR = xL
  21.         xL = b - k * (b - a)
  22.     else:
  23.         a = xL
  24.         xL = xR
  25.         xR = a + k * (b - a)
  26. result = float((b-a)/2)
  27. print('a: ' + str(a))
  28. print('b: ' + str(b))
  29. print('minimum tej funkcji jest w punkcie: [' + str(result) + ', ' + str(func(result)) + '] ')
Advertisement
Add Comment
Please, Sign In to add comment