# sos2

Tags:

Updated:

sos2 creates a special ordered set of type 2 constraint on a vector (at most two of the variables are non-zero, and the non-zero variables must be consecutive in their ordering)

## Syntax

F = sos2(X,AdjecencyWeights);


## Example

sos2 constraints are often used to model piecewise linear approximations of nonlinear functions. Consider the problem of minimizing $$sin(2\pi x_1) + cos(2\pi x_2)^3$$ subject to $$x_1+x_2 = 1$$. With sos2 based approximators, we have

xi = linspace(0,1,25)';
f1 = sin(2*pi*xi);
f2 = cos(2*pi*xi).^3;
plot(xi,f1,xi,f2)

sdpvar x1 x2 y1 y2
lambda1 = sdpvar(length(f1),1)
lambda2 = sdpvar(length(f2),1)
F = [sos2(lambda1), sos2(lambda2)]
F = [F, x1 == lambda1'*xi, y1 == lambda1'*f1,lambda1>=0,sum(lambda1)==1]
F = [F, x2 == lambda2'*xi, y2 == lambda2'*f2,lambda2>=0,sum(lambda2)==1]
F = [F, x1 + x2 == 1];
optimize(F,y1+y2)
value([x1 x2])
value(y1+y2)


With interp1 you can conveniently write this as

f = interp1(xi,f1,x1,'sos2') + interp1(xi,f2,x2,'sos2');
optimize([x1 + x2 == 1],f)
value([x1 x2])
value(f)