工程数学--上机实验一:一维寻优法(0.618 法)程序设计

封装好的golds函数:

function [xm,fm,aList,bList,alList,akList] = golds(f,a,b,tol)
% f: 待优化的目标函数
% a,b: 初始区间
% tol: 精度要求
% xm,fm: 最优解和相应的最优函数值

% 黄金分割比例
r = (sqrt(5)-1)/2;
% 初始值
L = b-a;
x1 = a + (1-r)*L;
x2 = a + r*L;
fx1 = f(x1);
fx2 = f(x2);

% 记录每次迭代的值
i = 1;
aList(i) = a;
bList(i) = b;
alList(i) = x1;
akList(i) = x2;

% 迭代计算
while L > tol
    if fx1 > fx2
        a = x1;
        x1 = x2;
        fx1 = fx2;
        x2 = a + r*(b-a);
        fx2 = f(x2);
    else
        b = x2;
        x2 = x1;
        fx2 = fx1;
        x1 = a + (1-r)*(b-a);
        fx1 = f(x1);
    end
    i = i+1;
    aList(i) = a;
    bList(i) = b;
    alList(i) = x1;
    akList(i) = x2;
    L = b-a;
end

% 输出结果
xm = (a+b)/2;
fm = f(xm);
end

然后调用 golds 函数:

% 定义函数
f = @(x) x^2 - sin(x);
% 定义区间和精度
a = 0; b = 1; tol = 1e-6;
% 调用 golds 函数求解
[xm,fm,aList,bList,alList,akList] = golds(f,a,b,tol);
% 输出结果
fprintf('The minimum point is %f, and the minimum value is %f.\n', xm, fm);
fprintf('The a values are:\n'); disp(aList);
fprintf('The b values are:\n'); disp(bList);
fprintf('The al values are:\n'); disp(alList);
fprintf('The ak values are:\n'); disp(akList);

输出结果如下:

The minimum point is 0.876733, and the minimum value is -0.459698.
The a values are:
         0
         0
         0
    0.381966011250105
    0.236067977499790
    0.145898033750315
    0.090169943749474
    0.055728090000842
    0.034441853748633
    0.021286236252019
    0.013155483496614
    0.008130752755405
    0.005024965740613
    0.003105787014792
    0.001919178725821
    0.001186608289235
    0.000732381425586
    0.000454227862104
    0.000278372834758
    0.000175854482297
    0.000102578032461
    0.000073276955836
The b values are:
    1.000000000000000
    0.618033988749895
    0.381966011250105
    0.618033988749895
    0.527864045000420
    0.381966011250105
    0.291796067500630
    0.236067977499790
    0.181835090000840
    0.145898033750315
    0.111982834752170
    0.069915254246814
    0.108396910257348
    0.084076061991545
    0.070374253494812
    0.058715270752791
    0.048068058861111
    0.039235212890173
    0.032042696432049
    0.026337311863419
    0.021981384914175
    0.020242715948036
The al values are:
         0.381966011250105
         0.236067977499790
         0.145898033750315
         0.618033988749895
         0.527864045000420
         0.381966011250105
         0.291796067500630
         0.236067977499790
         0.181835090000840
         0.145898033750315
         0.111982834752170
         0.069915254246814
         0.108396910257348
         0.084076061991545
         0.070374253494812
         0.058715270752791
         0.048068058861111
         0.039235212890173
         0.032042696432049
         0.026337311863419
         0.021981384914175
         0.020242715948036
The ak values are:
    0.618033988749895
    0.381966011250105
    0.236067977499790
    0.527864045000420
    0.454131926249457
    0.327826024999581
    0.252371922499263
    0.204874905000158
    0.158725110000630
    0.127965201249685
    0.098915600503356
    0.061066579745468
    0.060857962507227
    0.047310052257331
    0.039163521508602
    0.032514812494448
    0.026682500870536
    0.021760693711955
    0.017817332481058
    0.013878867350363
    0.013055421881667
    0.010761747913840

 

posted @ 2023-05-04 10:54  小彭先森  阅读(734)  评论(2编辑  收藏  举报