Golden Search Optimization Problem
4 views (last 30 days)
Show older comments
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 Comments
Aric Acius
on 21 Oct 2017
Might be 5 years late, but in your else statement you calculate f2 instead of g2.
Answers (0)
See Also
Categories
Find more on Introduction to Installation and Licensing 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!