Hi guys, i don't understand why in tha code bellow (which works just fine) i don't understand why tau (the golden number) is 0.381.. ? I mean everywhere i look the golden number is 1.618 and if i change the constant value from 0.381 in 1.618 the code doesn't find the minimum of the function anymore. Any help is welcome. Kind regards
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code golden.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% a -> lower bound of the design variable
% b -> upper bound of the design variable
% alpha -> midpoint of a and b
% falpha1 -> function value at x = alpha1
% falpha2 -> function value at x = alpha2
% epsilon -> constant used to terminate the algorithm
% abs -> absolute of a number, MATLAB function
% tau -> 2-golden number
%
clear all
clc
a = 40;
b = 90;
epsilon = 0.00001;
tau = 0.381967;
alpha1 = a*(1-tau) + b*tau;
alpha2 = a*tau + b*(1-tau);
falpha1 = func(alpha1);
falpha2 = func(alpha2);
fprintf(' a b \n')
fprintf('————————-\n')
for i = 1:100
fprintf(' %7.3f %8.3f \n',a,b)
if falpha1 > falpha2
a = alpha1;
alpha1 = alpha2;
falpha1 = falpha2;
alpha2 = tau*a + (1-tau)*b;
falpha2 = func(alpha2);
else
b = alpha2;
alpha2 = alpha1;
falpha2 = falpha1;
alpha1 = tau*b + (1-tau)*a;
falpha1 = func(alpha1);
end
if abs(func(alpha1)-func(alpha2)) < epsilon
break;
end
end
fprintf('————————-\n')
fprintf('x* = %7.3f Minimum = %8.3f\n',alpha1,func(alpha1))
fprintf('Number of function calls = %3d\n',2+i)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code func.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% objective function to be coded here
% different test functions
%
function fx = func(x)
fx = 204165.5/(330-2*x) + 10400/(x-20);
% fx = 3*x^4+(x-1)^2;
% fx = -4*x*sin(x);
% fx = 2*(x-3)^2+exp(0.5*x*x);
%fx = 3*(x)^2+12/(x^3)-5;
% fx = 2*x*x+16/x;
%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Best Answer