Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.38 KB | None | 0 0
  1. clc, clear variables, close all
  2.  
  3. f = @(x) x.^2 - 5 * x + 7;
  4.  
  5. x0 = [1]; % punkt startowy
  6. d = [0.1]; % baza ortogonalnych wektorów
  7. tau = 1; % początkowa dł. kroku
  8. beta = 0.5; % wsp. korekcyjny tau
  9. eps = 0.01; % wymagana dokładność
  10. n = length(x0); % liczba zmiennych niezależnych
  11.  
  12. % inicjalizacja zmiennych
  13. counter = 0;
  14. k = 1;
  15. q0 = f(x0);
  16. x = x0;
  17. xB0 = x0;
  18. xB = xB0;
  19. while 1
  20.     counter = counter + 1;
  21.     while 1
  22.         xp = x;
  23.         x = x + tau * d;
  24.         q = f(x);
  25.         if q < q0
  26.             q0 = q;
  27.         else
  28.             x = x - 2 * tau * d;
  29.             q = f(x);
  30.             if q < q0
  31.                 q0 = q;
  32.             else
  33.                 x = xp;
  34.             end
  35.         end
  36.        
  37.         if k  ~= n
  38.             k = k + 1;
  39.         else
  40.             break;
  41.         end
  42.     end
  43.    
  44.     if f(xB0) > q0
  45.         xB0 = x;
  46.     else
  47.         if counter == 1
  48.             disp('[Błąd] Zmień punkt początkowy!');
  49.             return;
  50.         else
  51.             x0 = xB0;
  52.             tau = beta * tau;
  53.             k = 1;
  54.             q0 = f(x0);
  55.             continue;
  56.         end
  57.     end
  58.    
  59.     x0 = 2 * xB - xB0;
  60.     xB0 = xB;
  61.    
  62.     if tau < eps
  63.         break;
  64.     end
  65. end
  66.  
  67. % wyświetl wynik
  68. fprintf('f( [ %s ] ) == ( %s )\n', disp_vector(x0), disp_vector(q0));
  69.  
  70.  
  71.  
  72. function [str] = disp_vector(v)
  73. str = ['' num2str(v)];
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement