取符号函数梯度时出错
Error when taking gradient of symbolic function
我在获取符号函数的梯度时出错。谁能告诉我为什么会出现此错误?
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])
我认为max部分给我带来了麻烦,但我仍然需要确定这个函数的梯度。有什么建议吗? (我想我可以手工完成,但如果不需要的话我宁愿不这样做)
直接取max(0,gi(x1,x2))
的梯度在matlab中是不可能的。相反,函数应该根据 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]);
我在获取符号函数的梯度时出错。谁能告诉我为什么会出现此错误?
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])
我认为max部分给我带来了麻烦,但我仍然需要确定这个函数的梯度。有什么建议吗? (我想我可以手工完成,但如果不需要的话我宁愿不这样做)
直接取max(0,gi(x1,x2))
的梯度在matlab中是不可能的。相反,函数应该根据 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]);