# 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.

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

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