无约束优化的最速下降算法(理论推导+实例及代码实现)

 

 二、代码实现

主程序:

% lec5_例1: 典型行为表格

clc; clear; close all
syms x_1 x_2 step;

f = 5*x_1^2+x_2^2+4*x_1*x_2-14*x_1-6*x_2+20;
k = lec5_01_dichotomy(f,x_1,x_2,step,[0,10],10^(-6));

方法1、2程序:

% 最速下降法-步长已知

function k = steepest_descent(f,x_1,x_2,step,start_point,thereshold)

grad_f = [diff(f,x_1),diff(f,x_2)];

k = 0;
% Q = [10,4;4,2]; 
current_point = start_point;
delta = subs(grad_f,[x_1,x_2],[current_point(1),current_point(2)]);
direction = -delta;

while norm(direction) > thereshold
    k = k+1;
    direction = -delta;
%   step = (direction*direction')/(direction*Q*direction');
    step = (direction(1)^2+direction(2)^2)/(2*(5*direction(1)^2+direction(2)^2+4*direction(1)*direction(2)));
    f_value= double(subs(f,[x_1,x_2],[current_point(1),current_point(2)]));
    result_string=sprintf('k=%d, x_1=%.6f, x_2=%.6f, d_1^k=%.6f, d_2^k=%.6f, ||d^k||=%.8f, step=%.4f f(x^k)=%.6f\n',...
        k,current_point(1),current_point(2),direction(1),direction(2),norm(direction),step,f_value);
    disp(result_string);
    
    current_point = double([current_point(1),current_point(2)]+step.*direction);
    delta = subs(grad_f,[x_1,x_2],[current_point(1),current_point(2)]);
    
end
end

方法3程序:

function k = dichotomy(f,x_1,x_2,step,start_point,thereshold)

grad_f = [diff(f,x_1),diff(f,x_2)];

k = 0;
current_point = start_point;
delta = subs(grad_f,[x_1,x_2],[current_point(1),current_point(2)]);
direction = -delta;

while norm(direction) > thereshold
    k = k+1;
    direction = -delta;
    % TODO 用二分法求步长
    x_val = [current_point(1),current_point(2)]+step.*direction;
    f_val = subs(f,[x_1,x_2],[x_val(1),x_val(2)]);
    step = abs(double(solve(diff(f_val,step))));
   
    f_value= double(subs(f,[x_1,x_2],[current_point(1),current_point(2)]));
    result_string=sprintf('k=%d, x_1=%.6f, x_2=%.6f, d_1^k=%.6f, d_2^k=%.6f, ||d^k||=%.8f, step=%.4f f(x^k)=%.6f\n',...
        k,current_point(1),current_point(2),direction(1),direction(2),norm(direction),step,f_value);
    disp(result_string);
    
    current_point = double([current_point(1),current_point(2)]+step.*direction);
    delta = subs(grad_f,[x_1,x_2],[current_point(1),current_point(2)]);
    syms step;
    
end
end

运行结果:

k=1, x_1=0.000000, x_2=10.000000, d_1^k=-26.000000, d_2^k=-14.000000, ||d^k||=29.52964612, step=0.0866 f(x^k)=60.000000

k=2, x_1=-2.252782, x_2=8.786963, d_1^k=1.379968, d_2^k=-2.562798, ||d^k||=2.91071234, step=2.1800 f(x^k)=22.222576

k=3, x_1=0.755548, x_2=3.200064, d_1^k=-6.355739, d_2^k=-3.422321, ||d^k||=7.21856659, step=0.0866 f(x^k)=12.987827

k=4, x_1=0.204852, x_2=2.903535, d_1^k=0.337335, d_2^k=-0.626480, ||d^k||=0.71152803, step=2.1800 f(x^k)=10.730379

k=5, x_1=0.940243, x_2=1.537809, d_1^k=-1.553670, d_2^k=-0.836592, ||d^k||=1.76458951, step=0.0866 f(x^k)=10.178542

k=6, x_1=0.805625, x_2=1.465322, d_1^k=0.082462, d_2^k=-0.153144, ||d^k||=0.17393410, step=2.1800 f(x^k)=10.043645

k=7, x_1=0.985392, x_2=1.131468, d_1^k=-0.379797, d_2^k=-0.204506, ||d^k||=0.43135657, step=0.0866 f(x^k)=10.010669

k=8, x_1=0.952485, x_2=1.113749, d_1^k=0.020158, d_2^k=-0.037436, ||d^k||=0.04251845, step=2.1800 f(x^k)=10.002608

k=9, x_1=0.996429, x_2=1.032138, d_1^k=-0.092842, d_2^k=-0.049992, ||d^k||=0.10544577, step=0.0866 f(x^k)=10.000638

k=10, x_1=0.988385, x_2=1.027806, d_1^k=0.004928, d_2^k=-0.009151, ||d^k||=0.01039370, step=2.1800 f(x^k)=10.000156

k=11, x_1=0.999127, x_2=1.007856, d_1^k=-0.022695, d_2^k=-0.012221, ||d^k||=0.02577638, step=0.0866 f(x^k)=10.000038

k=12, x_1=0.997161, x_2=1.006797, d_1^k=0.001205, d_2^k=-0.002237, ||d^k||=0.00254076, step=2.1800 f(x^k)=10.000009

k=13, x_1=0.999787, x_2=1.001920, d_1^k=-0.005548, d_2^k=-0.002987, ||d^k||=0.00630107, step=0.0866 f(x^k)=10.000002

k=14, x_1=0.999306, x_2=1.001662, d_1^k=0.000294, d_2^k=-0.000547, ||d^k||=0.00062109, step=2.1800 f(x^k)=10.000001

k=15, x_1=0.999948, x_2=1.000469, d_1^k=-0.001356, d_2^k=-0.000730, ||d^k||=0.00154031, step=0.0866 f(x^k)=10.000000

k=16, x_1=0.999830, x_2=1.000406, d_1^k=0.000072, d_2^k=-0.000134, ||d^k||=0.00015183, step=2.1800 f(x^k)=10.000000

k=17, x_1=0.999987, x_2=1.000115, d_1^k=-0.000332, d_2^k=-0.000179, ||d^k||=0.00037653, step=0.0866 f(x^k)=10.000000

k=18, x_1=0.999959, x_2=1.000099, d_1^k=0.000018, d_2^k=-0.000033, ||d^k||=0.00003711, step=2.1800 f(x^k)=10.000000

k=19, x_1=0.999997, x_2=1.000028, d_1^k=-0.000081, d_2^k=-0.000044, ||d^k||=0.00009204, step=0.0866 f(x^k)=10.000000

k=20, x_1=0.999990, x_2=1.000024, d_1^k=0.000004, d_2^k=-0.000008, ||d^k||=0.00000907, step=2.1800 f(x^k)=10.000000

k=21, x_1=0.999999, x_2=1.000007, d_1^k=-0.000020, d_2^k=-0.000011, ||d^k||=0.00002250, step=0.0866 f(x^k)=10.000000

k=22, x_1=0.999998, x_2=1.000006, d_1^k=0.000001, d_2^k=-0.000002, ||d^k||=0.00000222, step=2.1800 f(x^k)=10.000000

k=23, x_1=1.000000, x_2=1.000002, d_1^k=-0.000005, d_2^k=-0.000003, ||d^k||=0.00000550, step=0.0866 f(x^k)=10.000000

k=24, x_1=0.999999, x_2=1.000001, d_1^k=0.000000, d_2^k=-0.000000, ||d^k||=0.00000054, step=2.1800 f(x^k)=10.000000

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xmz_01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值