Guest User

Untitled

a guest
Apr 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.12 KB | None | 0 0
  1. function [minimum, error] = simplex(),
  2.     clf;
  3.     minimum = 1000;
  4.     epsilon = 0.0001;
  5.     n = 2;
  6.     l = 2.0;
  7.     x0 = [0.0; -1.0];
  8.     x = -20:.1:20;
  9.     y = -20:.1:20
  10.     % Формируем начальный симплекс.
  11.     r1 = l * (sqrt(n + 1.0) + n - 1.0) / (n * sqrt(2.0));
  12.     r2 = l * (sqrt(n + 1.0) - 1.0) / (n * sqrt(2.0));
  13.     x1 = [r1; r2];
  14.     x2 = [r2; r1];
  15.    
  16.     x_max = [0; 0];
  17.     x_mid = [0; 0];
  18.     x_min = [0; 0];
  19.     iterations = 0;
  20.     while iterations < 1000,
  21.         iterations += 1;
  22.         [x_min, x_mid, x_max] = sort(x0, x1, x2);
  23.  
  24.         x_cen = (x_min + x_mid) / 2.0;
  25.        
  26.         error = sqrt(0.5*((f(x_max) - f(x_min))^2.0 + (f(x_mid) - f(x_min))^2.0));
  27.         if error < epsilon
  28.             fprintf('\nDone...\n');
  29.             minimum = x_min;
  30.             x_min
  31.             hold off;
  32.             return;
  33.         end
  34.         % Отражение.
  35.         x_new = x_cen + (x_cen - x_max);
  36.  
  37.         if f(x_new) < f(x_max),        
  38.             x0 = x_new;
  39.             x1 = x_min;
  40.             x2 = x_mid;
  41.         end
  42.         if f(x_new) >= f(x_max),
  43.             x_max = (x_max + x_min).*0.5;
  44.             x_mid = (x_mid + x_min).*0.5;          
  45.             x0 = x_max;
  46.             x1 = x_min;
  47.             x2 = x_mid;
  48.         end
  49.        
  50.         x_min
  51.         steps_x = 0;
  52.         steps_y = 0;
  53.  
  54.     end
  55. end
Add Comment
Please, Sign In to add comment