필터 지우기
필터 지우기

How can I this code working

조회 수: 1 (최근 30일)
Seojin Kim
Seojin Kim 2023년 10월 15일
답변: Divit 2023년 11월 15일
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
Dyuman Joshi
Dyuman Joshi 2023년 10월 15일
What seems to be the problem?
And how are you calling the function? What are the input values?
Sam Chak
Sam Chak 2023년 10월 15일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Divit
Divit 2023년 11월 15일
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!

카테고리

Help CenterFile 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!

Translated by