📄 golden_section.m
字号:
function [mini,point,times]=golden_section(str,ini,acc)
% File name:golden_section.m
% Author:Yan Anxin
% ID number:081810
% Date:finished on 2008.11.24
% Description:
% Here defines a function,to solve the one-dimensional searching problems by golden-section-method
% In the syntax [mini,point,times]=golden_section(str,ini,acc),
% str----input,means the expression of objective function
% ini----input,sets the starting interval to ini
% err----input,sets admissible error
% mini=golden_section(str,ini,acc) attempts to find mini, i.e. the minimum of objective function
% [mini,point]=golden_section(str,ini,acc) returns point, i.e. the independent variable value corresponding to minimum
% [mini,point,times]=golden_section(str,ini,acc) returns times, i.e. the times of iterative
f=sym(str);
syms x
sca=(sqrt(5)-1)/2; %golden ratio
k=1;times=0; %setting initial value
a(k)=ini(1);b(k)=ini(2);
y(k+1)=a(k)+sca*(b(k)-a(k));
z(k+1)=a(k)+(1-sca)*(b(k)-a(k));
while 1 %to narrow the search interval
if subs(f,x,z(k+1))<=subs(f,x,y(k+1))
a(k+1)=a(k);b(k+1)=y(k+1);
if ((b(k+1)-a(k+1))/(b(1)-a(1)))<acc,break,end
y(k+2)=z(k+1);z(k+2)=a(k+1)+(1-sca)*(b(k+1)-a(k+1));
k=k+1;times=times+1;
else
a(k+1)=z(k+1);b(k+1)=b(k);
if ((b(k+1)-a(k+1))/(b(1)-a(1)))<acc,break,end
z(k+2)=y(k+1);y(k+2)=a(k+1)+sca*(b(k+1)-a(k+1));
k=k+1;times=times+1;
end
end
point=0.5*(a(k+1)+b(k+1));
mini=subs(f,x,point);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -