2024/5/26

所学时间:2小时

代码行数:500

博客园数:1篇

所学知识:

实验三:Newton法程序设计

一、实验目的

 

掌握Hesse矩阵的计算方法和Newton法的基本思想及其迭代步骤;学会运用MATLAB编程实现常用优化算法;能够正确处理实验数据和分析实验结果及调试程序。

 

二、实验内容

1)求解无约束优化问题:

2)终止准则取

3)完成Newton法(牛顿法)的MATLAB编程、调试;

4)选取几个与实验二中相同的初始点,并给出相关实验结果的对比及分析(从最优解、最优值、收敛速度(迭代次数)等方面进行比较);

 

三、算法步骤、代码、及结果

1. 算法步骤

 

2. 代码

function newtons_method()

    % 初始猜测

    x = [1; 1; 1; 1];

    

    % 设定迭代停止条件

    epsilon = 1e-6;

    

    % 迭代

    iter = 0;

    while true

        % 计算梯度和海森矩阵

        grad = [2*(x(1)+10*x(2))+40*(x(1)-x(4))^3; 20*(x(1)+10*x(2))+4*(x(2)-2*x(3))^3; 10*(x(3)-x(4))-8*(x(2)-2*x(3))^3; -10*(x(3)-x(4))-40*(x(1)-x(4))^3];

        hessian = [2+120*(x(1)-x(4))^2, 20, 0, -120*(x(1)-x(4))^2; 20, 200+12*(x(2)-2*x(3))^2, -24*(x(2)-2*x(3))^2, 0; 0, -24*(x(2)-2*x(3))^2, 10+48*(x(2)-2*x(3))^2, -10; -120*(x(1)-x(4))^2, 0, -10, 10+120*(x(1)-x(4))^2];

        

        % 计算步长

        step = hessian\grad;

        

        % 更新解

        x = x - step;

        

        % 计算目标函数值

        f_val = (x(1)+10*x(2))^2+5*(x(3)-x(4))^2+(x(2)-2*x(3))^4+10*(x(1)-x(4))^4;

        

        % 输出迭代信息

        disp(['迭代次数: ', num2str(iter), ': x = ', num2str(x'), ', f(x) = ', num2str(f_val)]);

        

        % 判断是否满足停止条件

        if norm(step) < epsilon

            disp(['最优解 x = ', num2str(x'), ', 最优值f(x) = ', num2str(f_val)]);

            break;

        end

        

        iter = iter + 1;

    end

    disp(['共迭代:',num2str(iter),'']);

end

 

3. 结果

迭代次数: 0: x = -1.5365     0.15365     0.41016     0.41016, f(x) = 143.7923

迭代次数: 1: x = -1.0243     0.10243     0.27344     0.27344, f(x) = 28.4034

迭代次数: 2: x = -0.68288    0.068288     0.18229     0.18229, f(x) = 5.6106

迭代次数: 3: x = -0.45525    0.045525     0.12153     0.12153, f(x) = 1.1083

迭代次数: 4: x = -0.3035     0.03035    0.081019    0.081019, f(x) = 0.21891

迭代次数: 5: x = -0.20233    0.020233    0.054012    0.054012, f(x) = 0.043242

迭代次数: 6: x = -0.13489    0.013489    0.036008    0.036008, f(x) = 0.0085417

迭代次数: 7: x = -0.089926   0.0089926    0.024006    0.024006, f(x) = 0.0016873

迭代次数: 8: x = -0.059951   0.0059951    0.016004    0.016004, f(x) = 0.00033328

迭代次数: 9: x = -0.039967   0.0039967    0.010669    0.010669, f(x) = 6.5834e-05

迭代次数: 10: x = -0.026645   0.0026645   0.0071128   0.0071128, f(x) = 1.3004e-05

迭代次数: 11: x = -0.017763   0.0017763   0.0047418   0.0047418, f(x) = 2.5687e-06

迭代次数: 12: x = -0.011842   0.0011842   0.0031612   0.0031612, f(x) = 5.0741e-07

迭代次数: 13: x = -0.0078948  0.00078948   0.0021075   0.0021075, f(x) = 1.0023e-07

迭代次数: 14: x = -0.0052632  0.00052632    0.001405    0.001405, f(x) = 1.9798e-08

迭代次数: 15: x = -0.0035088  0.00035088  0.00093666  0.00093666, f(x) = 3.9108e-09

迭代次数: 16: x = -0.0023392  0.00023392  0.00062444  0.00062444, f(x) = 7.7249e-10

迭代次数: 17: x = -0.0015595  0.00015595  0.00041629  0.00041629, f(x) = 1.5259e-10

迭代次数: 18: x = -0.0010396  0.00010396  0.00027753  0.00027753, f(x) = 3.0142e-11

迭代次数: 19: x = -0.00069309  6.9309e-05  0.00018502  0.00018502, f(x) = 5.9539e-12

迭代次数: 20: x = -0.00046206  4.6206e-05  0.00012335  0.00012335, f(x) = 1.1761e-12

迭代次数: 21: x = -0.00030804  3.0804e-05  8.2231e-05  8.2231e-05, f(x) = 2.3231e-13

迭代次数: 22: x = -0.00020536  2.0536e-05   5.482e-05   5.482e-05, f(x) = 4.5889e-14

迭代次数: 23: x = -0.00013691  1.3691e-05  3.6547e-05  3.6547e-05, f(x) = 9.0644e-15

迭代次数: 24: x = -9.1272e-05  9.1272e-06  2.4365e-05  2.4365e-05, f(x) = 1.7905e-15

迭代次数: 25: x = -6.0848e-05  6.0848e-06  1.6243e-05  1.6243e-05, f(x) = 3.5368e-16

迭代次数: 26: x = -4.0565e-05  4.0565e-06  1.0829e-05  1.0829e-05, f(x) = 6.9863e-17

迭代次数: 27: x = -2.7043e-05  2.7043e-06  7.2192e-06  7.2192e-06, f(x) = 1.38e-17

迭代次数: 28: x = -1.8029e-05  1.8029e-06  4.8128e-06  4.8128e-06, f(x) = 2.7259e-18

迭代次数: 29: x = -1.2019e-05  1.2019e-06  3.2085e-06  3.2085e-06, f(x) = 5.3846e-19

迭代次数: 30: x = -8.0129e-06  8.0129e-07   2.139e-06   2.139e-06, f(x) = 1.0636e-19

迭代次数: 31: x = -5.3419e-06  5.3419e-07   1.426e-06   1.426e-06, f(x) = 2.101e-20

迭代次数: 32: x = -3.5613e-06  3.5613e-07  9.5068e-07  9.5068e-07, f(x) = 4.1501e-21

迭代次数: 33: x = -2.3742e-06  2.3742e-07  6.3379e-07  6.3379e-07, f(x) = 8.1977e-22

迭代次数: 34: x = -1.5828e-06  1.5828e-07  4.2254e-07  4.2254e-07, f(x) = 1.6193e-22

最优解 x = -1.5828e-06  1.5828e-07  4.2254e-07  4.2254e-07, 最优值f(x) = 1.6193e-22

共迭代:34

四、心得体会

 

在实验中,我通过MATLAB编程实现了常用的优化算法,并通过调试程序验证了算法的正确性。在处理实验数据和分析实验结果时,我深入思考了不同算法对于不同问题的适用性,并通过对比实验结果找到了最优的优化算法。通过这次实验,我不仅提高了编程能力,也加深了对优化算法的理解和应用。总的来说,这次实验给我带来了很大收获,让我更加了解了优化算法的原理和实现方法,提高了对算法效率和精度的要求。

posted @ 2024-05-26 20:31  为20岁努力  阅读(3)  评论(0编辑  收藏  举报