Golden section search algorithm
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Errors :
with the same size and shape as the input arguments.
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function/FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine.set.Function_I
In matlab.graphics.function.FunctionLine.set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 245)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 200)
In fplot>vectorizeFplot (line 200)
In fplot (line 166)
In goldensection (line 30)
Matlab code :
clc; clear all; clc;
phi = double( (sqrt(5)-1) / 2 ); % golden ratio
del = .05;% small increment value
epsilon = .001; % function difference precision
max_iter = 100; % maximum number of iterations for Phase I and II.
alpha(1) = 0; % first value of alpha
alpha(2) = del; % second value of alpha is equal to the small increment value
% begin iterations for Phase I
i_iter = 1;
while i_iter <= max_iter
if ( f(alpha(i_iter)) > f(alpha(i_iter + 1)) )
alpha(i_iter + 2) = 0;
for j_iter = 1 : i_iter+1
alpha(i_iter + 2) = alpha(i_iter + 2) + del*(1.618)^(j_iter-1);
end
else
break
end
i_iter = i_iter + 1; % increment number of iterations by 1
end
% the uncertainity interval
alpha_l = 0; % set lower bound of alpha since it cannot be known how many alphas will be enough, i.e. alpha(i_iter - 2) may be null.
alpha_u = alpha(i_iter + 1);
% plot the line search function of one variable (i.e., alpha - a)
figure; hold on;
syms a;
fplot(@f,[alpha_l alpha_u],'b');
% plot title and axes labels
title( 'Golden section search on f ( alpha )' );
xlabel('alpha');
ylabel('f (alpha)');
% divide uncertainity interval using golden ratio, (i.e., set new interval
% boundaries)
a = alpha_l + (1 - phi)*(alpha_u - alpha_l);
b = alpha_l + phi*(alpha_u - alpha_l);
% calculate function values at points
f_a = f(a);
f_b = f(b);
% plot new interval boundaries on top of the function plot.
plot(a,f_a,'rd')
plot(b,f_b,'rd')
% begin iterations for Phase II
i_iter = 1;
while ( ( abs(alpha_u-alpha_l) > epsilon ) && (i_iter < max_iter) ) && (i_iter < max_iter)
if(f_a < f_b)
alpha_u = b;
b = a;
a = alpha_l + (1 - phi)*(alpha_u - alpha_l);
f_a=f(a);
f_b=f(b);
plot(a,f_a,'rd');
else
alpha_l = a;
a = b;
b = alpha_l + phi*(alpha_u - alpha_l);
f_a = f(a);
f_b = f(b);
plot(b, f_b, 'rd')
end
end
% print alpha value that minimizes the line search function
if(f_a<f_b)
fprintf('\nalpha_min = %f\n', a)
fprintf('f_min = %f\n', f_a)
plot(a,f_a,'kd', 'LineWidth', 1, 'MarkerSize', 10, 'MarkerFaceColor' , 'g')
else
fprintf('\nalpha_min = %f\n', b)
fprintf('f_min = %f\n', f_b)
plot(b,f_b,'kd', 'LineWidth', 1, 'MarkerSize', 10, 'MarkerFaceColor' , 'g')
end
hold off;
saveas(gcf,'results_plot.png')
function y = f(a)
% This function returns the value of the line search function for a given
% value of function variable.
% y = 2 - 4*a + exp(a); % Example 10.3 in the book (Arora)
% y = (11*a - 5)^2 + 16*a^2; % Homework 3 / Section B - a
% y = 0.1*(5 - a)^2 + (1 - 2*a)^2; % Homework 3 / Section B - b
y =(-19+2358*a)^2+5*(-1+588*a)^2+(-4+880*a)^4+10*(-3+2208*a)^4;
end
0 Kommentare
Antworten (1)
Pratyush Roy
am 31 Mär. 2021
Hi,
fplot prefers to pass a vector to the function for efficiency, but checks whether that works and gives a warning if it doesn't work. As a workaround, you can consider changing the function f in a manner as follows:
function y = f(a)
%Here we assume that a is an array and the function works on every element of a
%to return a corresponding element in the y array.
for i=1:numel(a)
y(i) =(-19+2358.*a(i)).^2 + 5*(-1+588.*a(i)).^2 + (-4+880.*a(i)).^4 + 10.*(-3+2208.*a(i)).^4;
end
end
Hope this helps!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!