采用渐变符号函数时出错

时间:2016-02-22 10:34:06

标签: matlab gradient

在获取符号函数的渐变时出现错误。谁能告诉我为什么会出现这个错误?

syms x1 x2 R

L0=0; %[m]
k1=8; %[N/m]
k2=4; %[N/m]
F1=5;   %[N]
F2=10;  %[N]

F = 0.5*k1*(sqrt(x1^2 + (L0-x2)^2) - L0)^2 + 0.5*k2*(sqrt(x1^2 + (L0+x2)^2)
    - L0)^2 - F1*x1 - F2*x2;
f = matlabFunction(F);

R = 0.1;
GI = x1^2 + x2^2 - R^2; 
gi = matlabFunction(GI);

epsilon=0.001;

xo=[0,0]';
k = 0;
r = (sqrt(5)-1) / 2;
rpe=0.01;

Merit_pe = @(x1,x2) f(x1,x2) + rpe*(max(0,gi(x1,x2)))^2;

g = gradient(Merit_pe, [x1 x2]);

错误:

Error using sym/max (line 97)

Input arguments must be convertible to
floating-point numbers.

Error in
Ex>@(x1,x2)f(x1,x2)+rpe*(max(0,gi(x1,x2)))^2

Error in sym>funchandle2ref (line 1249)
    S = x(S{:});

Error in sym>tomupad (line 1154)
    x = funchandle2ref(x);

Error in sym (line 163)
                S.s = tomupad(x);

Error in sym/gradient (line 17)
args = privResolveArgs(sym(f));

Error in Ex (line 31)
g = gradient(Merit_pe, [x1 x2])

我认为最大部分会给我带来麻烦,但我仍然需要确定此功能的渐变。有什么建议吗? (我想我可以手工完成,但如果我不需要,我宁愿不这样做)

1 个答案:

答案 0 :(得分:1)

在matlab中无法直接取max(0,gi(x1,x2))的渐变。相反,应该根据following definition来定义函数。

然后可以按如下方式定义函数Merit_pe

if gi(xo(1,1),xo(2,1)) > 0
    Merit_pe = @(x1,x2) f(x1,x2) + rpe*(gi(x1,x2))^2;
else
    Merit_pe = @(x1,x2) f(x1,x2);
end

然后可以使用以下方法确定渐变:

g = gradient(Merit_pe, [x1 x2]);