Golden Search Optimization Technique
89 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Alexander
2012-4-5
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
更多回答(4 个)
Aslain
2022-8-7
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 个评论
Walter Roberson
2022-8-7
https://www.mathworks.com/matlabcentral/answers/34570-golden-search-optimization-technique#answer_43383 but reverse the > to < because you are looking for minimum
Aslain
2022-8-9
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
0 个评论
Aslain
2022-8-9
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.
0 个评论
Aslain
2022-8-9
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!