Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc, clear variables, close all
- f = @(x) x.^2 - 5 * x + 7;
- x0 = [1]; % punkt startowy
- d = [0.1]; % baza ortogonalnych wektorów
- tau = 1; % początkowa dł. kroku
- beta = 0.5; % wsp. korekcyjny tau
- eps = 0.01; % wymagana dokładność
- n = length(x0); % liczba zmiennych niezależnych
- % inicjalizacja zmiennych
- counter = 0;
- k = 1;
- q0 = f(x0);
- x = x0;
- xB0 = x0;
- xB = xB0;
- while 1
- counter = counter + 1;
- while 1
- xp = x;
- x = x + tau * d;
- q = f(x);
- if q < q0
- q0 = q;
- else
- x = x - 2 * tau * d;
- q = f(x);
- if q < q0
- q0 = q;
- else
- x = xp;
- end
- end
- if k ~= n
- k = k + 1;
- else
- break;
- end
- end
- if f(xB0) > q0
- xB0 = x;
- else
- if counter == 1
- disp('[Błąd] Zmień punkt początkowy!');
- return;
- else
- x0 = xB0;
- tau = beta * tau;
- k = 1;
- q0 = f(x0);
- continue;
- end
- end
- x0 = 2 * xB - xB0;
- xB0 = xB;
- if tau < eps
- break;
- end
- end
- % wyświetl wynik
- fprintf('f( [ %s ] ) == ( %s )\n', disp_vector(x0), disp_vector(q0));
- function [str] = disp_vector(v)
- str = ['' num2str(v)];
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement