数学中,海森矩阵Hessian matrix 或 Hessian)是一个自变量为向量的实值函数的二阶偏导数组成的方块矩阵,此函数如下:

海森矩阵

如果 f 所有的二阶导数都存在,那么 f 的海森矩阵即:

海森矩阵

其中 海森矩阵,即

海森矩阵

(也有人把海森定义为以上矩阵的行列式) 海森矩阵被应用于牛顿法解决的大规模优化问题。

首先把变量名改成 x1 x2 ,并且确保之前 syms 过,即在之前写上:

syms x1 x2;

也就是 f 的表达式变成:

f = (x1 + x2) ^ 4 + x2 ^ 2

然后,梯度和海森矩阵分别为:

g = jacobian(f);
h = jacobian(g);

 

function H=hessian(f,x,x0)

% H=hessian(f,x) 计算表达式
% H=hessian(f,x,x0) 计算hessian矩阵的值 x0为x的初值

%$copyright by$ LUO sir

switch  nargin
    case 1
    error('please input variables in f(x)')

    case 2
        H=subhessian(f,x);
 
    case 3
        H=subhessian(f,x,x0);
        x=x0;
        H=subs(H);
    otherwise
       
         error('too many arguments or nothing')         
end

function HH=subhessian(f,x,x0)
 
n=length(x);

J=jacobian(f,x);
HH=[];
HH=sym(HH);
for i=1:n
HH(i,:)=jacobian(J(1,i),x);
end

 

 

相关文章:

  • 2021-12-26
  • 2021-10-02
  • 2022-12-23
  • 2022-01-17
  • 2021-07-01
  • 2021-12-08
猜你喜欢
  • 2022-01-07
  • 2022-12-23
  • 2021-07-30
  • 2021-11-01
  • 2021-08-04
相关资源
相似解决方案