• 二次方程优化(二)之 Quadratic Form


    =================2017-11-08==============

    MATLAB版本低,没有求hessian矩阵的函数,写了一个,需要的拿走

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % --------------------------
    %INPUT
    %f: funtion;
    %X: variables vactor;
    %--------------------------
    %OUTPUT
    %H: Hessian matrix
    %--------------------------
    %Example 
    %syms x y z
    %f = x^2 + y^2 + z^2;
    %X = [x, y, z];
    %H = hessian(f, X)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    funtion H = hessian(f, X)
    H = zeros(length(X));
    for i = 1: length(X)
      df1 = diff(f, X(i));
      for j = 1: length(X)
          df2 = diff(df1, X(j));
          H(i, j) = df2;
      end
    end
    

     ======================给一个求偏导,也就是第一步解偏导方程等于0的函数===================

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % --------------------------
    %INPUT
    %f: funtion;
    %X: variables vactor;
    %--------------------------
    %OUTPUT
    %op: optimal point
    %--------------------------
    %Example 
    %f = 2*x1^2 - 24 * x1 + 2 * x2^2 - 8*x2 + 2 * x3^2- 12*x3 + 200;
    %X = [x1, x2, x3];
    %op = first_condition(f, X)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function op = first_condition(f, X)
    constraint = [];
    for i = 1: length(X)
        df = diff(f, X(i));
        constraint = [constraint, df];
    end
    op1 = solve(constraint);
    op = [];
    for i = 1: length(X)
        s = ['op1.' char(X(i))];
        a = eval(s);
        op = [op, a];
    end
    disp(constraint.')
    

     ===========================下面给出用法和输出================

    比如说求极值,第一步是偏导等于零的点

    给出代码如下:

    syms x1 x2 x3
    f = 2*x1^2 - 24 * x1 + 2 * x2^2 - 8*x2 + 2 * x3^2- 12*x3 + 200;
    X = [x1, x2, x3];
    op = first_condition(f, X)
    

     输出结果:

    就是说在点(6, 2, 3)处取极值

    ============================更==========================

    我们这一次对二次型进行更深的讨论

    讨论多变量的正定型、负定型和半正定、半负定

    先给出结论:

    判断有两种方法:

    1.强推特征值法:如果所有的特征值是正,则是正定型;全是负的就是负定型;全是非负,就是半正定型;全是非正,就是半负定型;

    2.子式判断法:分两步:

      第一步:所有阶的主子式大于0,就是正定型;奇数阶的主子式小于0并且偶数阶的主子式大于0,就是负定型;所有阶的主子式大于等于0或者奇数阶的主子式小于等于0并且偶数阶的主子式大于等于0,转入第二步;除此之外的都是不定型;

      第二步:所有阶的子式大于等于0,就是半正定型;奇数阶的子式小于等于0并且偶数阶的子式大于等于0,就是半负定型

    在继续下面的讨论时,我想先插入一小段关于相关矩阵的一些说明

  • 相关阅读:
    PC上图标显示异常 白色框框处理方案
    判断Javascript变量是否为空 undefined 或者null(附样例)
    MySQL双版本共存解决方案
    跨境电商进口业务的通关模式
    史上最完整跨境电商投资分析报告!
    Fastcgi_Finish_Request 提高页面响应速度
    浏览器工作原理
    thinkphp 命名规范
    git diff ^M的消除
    git简明教程
  • 原文地址:https://www.cnblogs.com/Mr-ZeroW/p/7748581.html
Copyright © 2020-2023  润新知