Advertisement
DMG

Golden ratio method

DMG
Nov 29th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.63 KB | None | 0 0
  1. function x = golden_ratio_method(a, b, max_error, max_iter, my_function)
  2.  
  3.     c = (3 - sqrt(5))/2;
  4.  
  5.     for i = 1:max_iter
  6.        
  7.         x_1 = a + c*(b - a);
  8.         x_2 = b - c*(b - a);
  9.        
  10.         if feval(my_function, x_1) > feval(my_function, x_2)
  11.             a = x_1;
  12.         elseif feval(my_function, x_2) > feval(my_function, x_1)
  13.             b = x_2;
  14.         else
  15.             % Novi par tacaka ?
  16.             a = x_1;
  17.             b = x_2;
  18.         end
  19.        
  20.         if abs(b-a) < max_error
  21.             x = (a+b)/2;
  22.             return;
  23.         end
  24.        
  25.     end
  26.    
  27.     x = (a+b)/2;
  28.        
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement