Clear Filters
Clear Filters

How can I this code working

2 views (last 30 days)
Seojin Kim
Seojin Kim on 15 Oct 2023
Answered: Divit on 15 Nov 2023
function [al, aa, au fl, fa, fu, nFuncCall]=GoldenSectionPhaseI(func,X0,d,delta)
q=0;
a1=0;
a2=delta;
nFuncCall=0;
F1=func(X0+a1*d);
nFuncCall=nFuncCall+1;
F2=func(X0+a2*d);
nFuncCall=nFuncCall+1;
while(1)
q=q+1;
a3=0;
for j=0:q
a3=a3+(1.618)^j*delta;
end
F3=func(X0+a3*d);
if (F1>F2 && F2<F3)
break
else
a1=a2; F1=F2;
a2=a3; F2=F3;
end
end
al=a1;
aa=a2;
au=a3;
fl=F1;
fa=F2;
fu=F3;
end
  2 Comments
Dyuman Joshi
Dyuman Joshi on 15 Oct 2023
What seems to be the problem?
And how are you calling the function? What are the input values?
Sam Chak
Sam Chak on 15 Oct 2023
Looks like the Golden Ratio section root-finding technique. Perhaps OP wants to test it out on some nonlinear functions, but doesn't know how to call the function.

Sign in to comment.

Answers (1)

Divit
Divit on 15 Nov 2023
Hi Seojin,
I understand that you would like to test and run the code you provided.
The code you provided implements the Golden Section Search method for finding the minimum of a unimodal function. However, there are a few syntax errors and missing variable declarations in the code you provided. Here's the corrected code:
Here's how you can use this function:
  1. Define your objective function "func" that takes a vector input and returns a scalar output.
  2. Define your initial point "X0" as a vector.
  3. Define the search direction "d" as a vector.
  4. Define the initial step size "delta".
  5. Call the "GoldenSectionPhaseI" function with the appropriate arguments.
You can refer to the following example code:
% Define the objective function
func = @(x) 100 * (x(2) - x(1)^2)^2 + (1 - x(1))^2; % Example objective function: Rosenbrock's function(replace with your own)
% Define the initial point and search direction as 2-dimensional vectors
X0 = [0; 0]; % Initial point
d = [1; 1]; % Search direction
% Define the initial step size
delta = 0.1;
% Call the GoldenSectionPhaseI function
[al, aa, au, fl, fa, fu, nFuncCall] = GoldenSectionPhaseI(func, X0, d, delta);
% Display the results
disp(['al: ', num2str(al)]);
al: 0.52359
disp(['aa: ', num2str(aa)]);
aa: 0.94717
disp(['au: ', num2str(au)]);
au: 1.6325
disp(['fl: ', num2str(fl)]);
fl: 6.4492
disp(['fa: ', num2str(fa)]);
fa: 0.25316
disp(['fu: ', num2str(fu)]);
fu: 107.0291
disp(['nFuncCall: ', num2str(nFuncCall)]);
nFuncCall: 2
function [al, aa, au, fl, fa, fu, nFuncCall] = GoldenSectionPhaseI(func, X0, d, delta)
q = 0;
a1 = 0;
a2 = delta;
nFuncCall = 0;
F1 = func(X0 + a1 * d);
nFuncCall = nFuncCall + 1;
F2 = func(X0 + a2 * d);
nFuncCall = nFuncCall + 1;
while true
q = q + 1;
a3 = 0;
for j = 0:q
a3 = a3 + (1.618)^j * delta;
end
F3 = func(X0 + a3 * d);
if (F1 > F2 && F2 < F3)
break
else
a1 = a2;
F1 = F2;
a2 = a3;
F2 = F3;
end
end
al = a1;
aa = a2;
au = a3;
fl = F1;
fa = F2;
fu = F3;
end
Feel free to replace the objective function with your own function that you want to minimize using the Golden Section Search method. Make sure to adjust the dimensions of "X0" and "d" based on your specific optimization problem. For example, if your objective function takes a vector of two variables, you would need to set "X0" and "d" as 2-dimensional vectors.
I hope it helps!

Tags

Community Treasure Hunt

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

Start Hunting!