梯度和粗糙度问题的函数句柄MATLAB

时间:2014-02-21 08:21:22

标签: matlab function gradient handle hessian-matrix

我在操作渐变和粗麻布的函数句柄时遇到了问题。

我有以下代码:

syms x1 x2

x = [x1,x2];

% Define the function phi(x)
phi = @(x1,x2) 10*x1^4 - 20*x1^2*x2 + 10*x2^2 + x1^2 - 2*x1 + 5; 

% Define the gradient of the function phi(x)
gradphi = @(x1,x2) jacobian(phi,x).';

% Define the Hessian of phi(x)    
hessphi = @(x1,x2) jacobian(gradphi,x);

现在当我输入命令终端时:

phi(1,2)

我得到一些标量值。

但是当我输入

gradphi(1,2)
hessianphi(1,2)

我希望在这些点评估渐变的相应矢量。

对于渐变,我得到

EDU>> gradphi(1,2)

ans =

2*x1 - 40*x1*x2 + 40*x1^3 - 2
            - 20*x1^2 + 20*x2

这只是渐变矢量函数。但我希望输入x1 = 1和x2 = 2的实际数值结果。

EDU>> hessphi(1,2)

返回错误。

我不确定为什么。

1 个答案:

答案 0 :(得分:2)

对于数组的乘法和除法,您需要在.*和{{1}的定义中使用逐元素运算符./.^hessianphi }。否则,Matlab将尝试做矩阵乘法/除法/幂,这将不会顺利。

/ aside:搜索错误消息的文本将显示最可能的错误原因。