Golden Search Optimization Technique

89 visualizaciones (últimos 30 días)
Jacob
Jacob el 5 de Abr. de 2012
Respondida: Aslain el 9 de Ag. de 2022
Hi all. I am trying to find the maximum value of the function using the Golden Search algorithm. I have double-checked through my calculator, and the maximum value is at x=1.0158527. However, that is now what I get on my program. I am wondering if someone can help where I am going wrong.Here is the code and the results i get
%%%Author: Jacob Joshua Shila
%%%Golden Search Algorithm
clear all
clc
format short e
syms x
%%Input
fx = 0.4/sqrt(1+x^2)-sqrt(1+x^2)*(1-.4/(1+x^2))+x;
maxit = 50;
es = 10^-5;
R = (5^.5-1)/2;
%%Determine the Interval for the Initial Guess
x=[-10:10];
f = subs(fx,x);
plot(x,f);
xlow = 0.5;
xhigh = 1.5;
%%Perform Golden Search
xl = xlow;
xu = xhigh;
iter = 1;
d = R*(xu-xl);
x1 = xl+d;
x2 = xu-d;
f1 = subs(fx,x1);
f2 = subs(fx,x2);
if f1>f2
xopt = x1;
fx = f1;
else
xopt = x2;
fx = f2;
end
while(1)
d = R*d;
if f1>f2
xl = x2;
x2 = x1;
x1 = xl+d;
f2 = f1;
f1 = subs(fx,x1);
else
xu = x1;
x1 = x2;
x2 = xu-d;
f1 = f2;
f2 = subs(fx,x2);
end
iter = iter+1;
if f1>f2
xopt = x1;
fx = f1;
else
xopt = x2;
fx = f2;
end
if xopt~=0
ea = (1-R)*abs((xu-xl)/xopt)*100;
end
if ea<=es||iter>=maxit,break
end
end
Gold = xopt
and here is the results which I get from the program:
Gold =
8.8197e-001
Thank you and any help appreciated.

Respuesta aceptada

Alexander
Alexander el 5 de Abr. de 2012
I'm not familiar with the Golden Search algorithm, but it seems that you are overwritting fx by accident. If I remove those line, it gives a more plausible result:
%%%Author: Jacob Joshua Shila
%%%Golden Search Algorithm
clear all
clc
syms x
%%Input
fx = 0.4/sqrt(1+x^2)-sqrt(1+x^2)*(1-.4/(1+x^2))+x;
maxit = 50;
es = 10^-5;
R = (5^.5-1)/2;
%%Determine the Interval for the Initial Guess
x=[-10:10];
f = subs(fx,x);
xlow = 0.5;
xhigh = 1.5;
%%Perform Golden Search
xl = xlow;
xu = xhigh;
iter = 1;
d = R*(xu-xl);
x1 = xl+d;
x2 = xu-d;
f1 = subs(fx,x1);
f2 = subs(fx,x2);
if f1>f2
xopt = x1;
else
xopt = x2;
end
while(1)
d = R*d;
if f1>f2
xl = x2;
x2 = x1;
x1 = xl+d;
f2 = f1;
f1 = subs(fx,x1);
else
xu = x1;
x1 = x2;
x2 = xu-d;
f1 = f2;
f2 = subs(fx,x2);
end
iter = iter+1;
if f1>f2
xopt = x1;
else
xopt = x2;
end
if xopt~=0
ea = (1-R)*abs((xu-xl)/xopt)*100;
end
if ea<=es||iter>=maxit,break
end
end
Gold = xopt
Returns:
Gold =
1.0519
I believe this is the correct result. You get the same result if you take the derivative and search for its roots:
>> double(solve(diff(fx), 'Real', true))
ans =
1.0519
  2 comentarios
Jacob
Jacob el 8 de Abr. de 2012
Thank for the input, it now works fine.
ilyada
ilyada el 18 de Abr. de 2018
How can we create a plot for this function?

Iniciar sesión para comentar.

Más respuestas (4)

Aslain
Aslain el 7 de Ag. de 2022
Hello,
I am looking for both the minimum of a function and the value at which this minimum is reached by the Golden Search method.
I need you to help me with the complete code directly implementable on matlab.
The function in question is the following with its various parameters.
f(x)=(((x.^(beta-1))*cc*A0)/(k*(nu^beta)))+(A2./(x*T))
cc=150000;
cp=800000;
cov=6000000;
k=5;
alfa=0.8;
beta=3.3;
nu=2100;
A0=1;
for j=1:k-1
A0=A0+exp(j*alfa);
end
A2=(k-1)*cp+cov.
Additional data for this function is as follows:
a=0; % start of interval
b=2; % end of interval
epsilon=0.000001; % accuracy value
iter= 100; % maximum number of iterations
r = (sqrt(5) - 1)*0.5; % golden proportion coefficient, around 0.618
I really ask you to help me, because I spent several sleepless nights without finding solutions.
  1 comentario
Walter Roberson
Walter Roberson el 7 de Ag. de 2022
https://www.mathworks.com/matlabcentral/answers/34570-golden-search-optimization-technique#answer_43383 but reverse the > to < because you are looking for minimum

Iniciar sesión para comentar.


Aslain
Aslain el 9 de Ag. de 2022
I followed your instructions to the letter, but unfortunately it still does not give me the correct minimal solution and the exact postion of this solution.
Indeed by direct derivation, the minimum obtained is indeed 1460.5 obtained at the position of x=1807.6 .
When I implement the code proposed for my case for minimization (respecting the change of inequalities that you had proposed to me), I obtain as minimum value of the function, the value 1.9534e+003 , obtained for x= 999.9999 .
There is indeed a strong disparity around the optimal solution (given by the direct derivation).
Can you offer me the correct full code?
The code I implemented for my case is as follows:
%%%Golden Search Algorithm
clear all
clc
syms x
%%Input
cc=150000;
cp=800000;
cov=6000000;
k=5;
alfa=0.8;
beta=3.3;
nu=2100;
A0=1;
for j=1:k-1
A0=A0+exp(j*alfa);
end
A2=(k-1)*cp+cov;
fx=(((x.^(beta-1))*cc*A0)/(k*(nu^beta)))+(A2./(k*x));
maxit = 100;
es = 10^-5;
R = (5^.5-1)/2;
%%Determine the Interval for the Initial Guess
x=[0:5000];
f = subs(fx,x);
xlow = 100;
xhigh = 1000;
%%Perform Golden Search
xl = xlow;
xu = xhigh;
iter = 1;
d = R*(xu-xl);
x1 = xl+d;
x2 = xu-d;
f1 = subs(fx,x1);
f2 = subs(fx,x2);
if f1<f2
xopt = x1;
else
xopt = x2;
end
while(1)
d = R*d;
if f1<f2
xl = x2;
x2 = x1;
x1 = xl+d;
f2 = f1;
f1 = subs(fx,x1);
else
xu = x1;
x1 = x2;
x2 = xu-d;
f1 = f2;
f2 = subs(fx,x2);
end
iter = iter+1;
if f1<f2
xopt = x1;
else
xopt = x2;
end
if xopt~=0
ea = (1-R)*abs((xu-xl)/xopt)*100;
end
if ea<=es||iter>=maxit,break
end
end
Gold = xopt

Aslain
Aslain el 9 de Ag. de 2022
I apologize, actually I was wrong. I set xhigh=1000 instead of xhigh=5000.
Your code works fine.
Thank you very much for your help, God bless you.

Aslain
Aslain el 9 de Ag. de 2022
I am dealing with a maintenance problem (reliability calculation...) according to the weibull model, to draw the point cloud
I consider the sample X of the TBFs, (time between failures)
X=[ ]
wblplot(X)
and it gives me the scatter plot on the logarithmic weibull plot
the problem is how to have the parameters of weibull, (beta, gamma and bare) on MATLAB, with which command, function??
Thank you for your help with an example please

Categorías

Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by