0
votes

I get an error when taking the gradient of a symbolic function. Can anyone tell me why I get this error?

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:

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])

I think the max part is causing me trouble, but I still need to determine the gradient of this function. Any advise? (I could do it by hand I guess, but I'd rather not if I don't have to)

1
You could replace max(0,x) by x * (x>0). Then I still get some MuPAD error, but maybe this idea can help you... - hbaderts

1 Answers

1
votes

Directly taking the gradient of max(0,gi(x1,x2)) is not possible in matlab. Instead the function should be defined according to the following definition.

The function Merit_pe can then be defined as follows:

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

The gradient can then be determined using:

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