Advertisement
MikecIT

hookJeeves

Nov 23rd, 2018
3,635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.43 KB | None | 0 0
  1. function [x, fx, cnt] = hookJeeves(fun, x0, d, dmin)
  2.     n = length(x0);
  3.     e = eye(n) * d;
  4.     x = x0;
  5.     fx = feval(fun, x);
  6.     cnt = 1;
  7.  
  8.     while e(1, 1) > dmin
  9.         t = x;
  10.         for i = 1:n
  11.             z = t + e(:, i);
  12.             y = feval(fun, z);
  13.             cnt = cnt + 1;
  14.             if y < fx
  15.             t = z;
  16.             fx = y;
  17.             else
  18.                 z = t - e(:, i);
  19.                 y = feval(fun, z);
  20.                 cnt = cnt + 1;
  21.                 if y < fx
  22.                     t = z;
  23.                     fx = y;
  24.                 end
  25.             end
  26.         end
  27.         if all(t == x)
  28.             e = e * 0.5;
  29.         else
  30.             x1 = t + (t - x);
  31.             f1 = feval(fun, x1);
  32.             cnt = cnt + 1;
  33.             x = t;
  34.             if f1 < fx
  35.                 for i = 1:n
  36.                     z = x1 + e(:, i);
  37.                     y = feval(fun, z);
  38.                     cnt = cnt + 1;
  39.                     if y < f1
  40.                         x = z;
  41.                         fx = y;
  42.                         break;
  43.                     end
  44.                     z = x1 - e(:, i);
  45.                     y = feval(fun, z);
  46.                     cnt = cnt + 1;
  47.                     if y < f1
  48.                         x = z;
  49.                         fx = y;
  50.                         break;
  51.                     end
  52.                 end
  53.             end
  54.         end
  55.     end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement