一维搜索的最优方法(黄金分割法)matlab程序

来源:互联网 发布:流程图制作软件 mac 编辑:程序博客网 时间:2024/06/09 16:58

这里写图片描述
例题:
用0.618法求解下列问题:

minex+x2

要求最终区间长度L0.2,初始区间为[0,1]。

执行文件:executegoldenOpt.mclear all;clc%% the original conditionsf=@(x)exp(-x)+x^2;a=0;b=1;Theta_error=0.2;%% execute goldenOpt Algorithm[x_opt,f_opt,stepNum] = goldenOpt(f,a,b,Theta_error)%% function figurex=a:0.01:b;y=exp(-x)+x.^2;plot(x,y,'k')hold onplot(x_opt,f_opt,'r*')fprintf('%d is the optimal point of the function and execute %d steps',x_opt,stepNum)
算法文件:goldenOpt.mfunction [x_opt,f_opt,stepNum] = goldenOpt(f,a,b,Theta_error)r=(sqrt(5)-1)/2;a1=b-r*(b-a);a2=a+r*(b-a);stepNum=0;while abs(b-a)>Theta_error    stepNum=stepNum+1;    f1=feval(f,a1);    f2=feval(f,a2);    if f1>f2       a=a1;       f1=f2;       a1=a2;       a2=a+r*(b-a);         else       b=a2;       a2=a1;       f2=f1;       a1=b-r*(b-a);           end    x_opt=(a+b)/2;   f_opt=feval(f,x_opt); end
0 0
原创粉丝点击