Golden Search Optimization Problem
显示 更早的评论
Hello everyone! I've been trying to figure out how to code the Golden Search algorithm for the function G with an initial interval of [-2,4]. However, I've been getting odd answers when I compared it with Newton's method. I was wondering if there was anything wrong with my code.
G = 4*x - (1.8*x^2) + (1.2*x^3) - (.3*x^4)
%Golden search method
%Define the parameters
phi = (1 + sqrt(5))/2;
a = -2;
b = 4;
n = 0;
c1 = (phi - 1)*a + (2-phi)*b;
c2 = (2-phi)*a + (phi - 1)*b;
G = @(x)(4*x - (1.8*x^2) + (1.2*x^3) - (.3*x^4));
g1 = G(c1);
g2 = G(c2);
while abs(b-a) > 0.0001
n = n+1;
if g1 < g2
b = c2;
c2 = c1;
g2 = g1;
c1 = (phi - 1)*a + (2 - phi)*b;
g1 = G(c1);
else
a = c1;
c1 = c2;
g1 = g2;
c2 = (2 - phi)*a + (phi - 1)*b;
f2 = G(c2);
end
end
for a comparison, I wrote a simple newton's method algorithm.
%Initial guess
x = 3;
%Desired tolerance and dummy tolerance to start iteration
Tol = 0.0001;
dummytol = 100;
while dummytol > Tol
X = x;
G = 4*x - (1.8*x^2) + (1.2*x^3) - (.3*x^4);
%Use derivative of the function to find the new volume
dG = 4 - 3.6*x + 3.6*x^2 - 1.2*x^3;
x = x - G/dG;
%Replace dummy tolerance with new tolerance
dummytol = abs(x - X);
end;
I got a maximum of 3.39 for newtons and -2 for the golden search. Thank you for any help!
3 个评论
SERGIO RODRIGUEZ
2013-4-27
You implement a minimization problem with the golden search....The result its reasonable.
SERGIO RODRIGUEZ
2013-4-27
And.. phi = (-1 + sqrt(5))/2;
Aric Acius
2017-10-21
Might be 5 years late, but in your else statement you calculate f2 instead of g2.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!