How can I this code working
2 次查看(过去 30 天)
显示 更早的评论
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
2023-10-15
What seems to be the problem?
And how are you calling the function? What are the input values?
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 个)
Omega
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:
- Define your objective function "func" that takes a vector input and returns a scalar output.
- Define your initial point "X0" as a vector.
- Define the search direction "d" as a vector.
- Define the initial step size "delta".
- 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)]);
disp(['aa: ', num2str(aa)]);
disp(['au: ', num2str(au)]);
disp(['fl: ', num2str(fl)]);
disp(['fa: ', num2str(fa)]);
disp(['fu: ', num2str(fu)]);
disp(['nFuncCall: ', num2str(nFuncCall)]);
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!
0 个评论
另请参阅
类别
在 Help Center 和 File 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!