Golden Search Optimization Technique

89 views (last 30 days)
Jacob
Jacob on 5 Apr 2012
Answered: Aslain on 9 Aug 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.

Accepted Answer

Alexander
Alexander on 5 Apr 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 Comments
Jacob
Jacob on 8 Apr 2012
Thank for the input, it now works fine.
ilyada
ilyada on 18 Apr 2018
How can we create a plot for this function?

Sign in to comment.

More Answers (4)

Aslain
Aslain on 7 Aug 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 Comment
Walter Roberson
Walter Roberson on 7 Aug 2022
https://www.mathworks.com/matlabcentral/answers/34570-golden-search-optimization-technique#answer_43383 but reverse the > to < because you are looking for minimum

Sign in to comment.


Aslain
Aslain on 9 Aug 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 on 9 Aug 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 on 9 Aug 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

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!