# How to make some optimization variables equal to each other?

6 views (last 30 days)

Show older comments

I want that some spesific variables that are to be optimized, are to be equal to each other.

lets say that i want this: x(1)=x(2), x(3)=x(4)=x(5), x(6)=x(7)

so whatever value x(1) is optimized to, has to have the same value as x(2).

The variables that needs to be equal to each other are separated by the number 0 in a matrix:

A=[x(1), x(2) 0 x(3), x(4), x(5), 0, 0, 0, x(6), x(7) 0];

how to do this aumtomatically in matlab?

##### 1 Comment

### Accepted Answer

Jon
on 15 Apr 2019

I assume you are using one of the MATLAB optimization functions such as fmincon or linprog. These functions include arguments Aeq,beq for equality constraints. I'm not quite clear about your indexing and the dimension of the vector x that you are trying to optimize. I think you are looking for a length twelve vector where some of the values are forced to zero and others must be equal to each other. If this is the case you could use these to enforce the constraints that you have, for example as follows:

Aeq = [1 -1 0 0 0 0 0 0 0 0 0 0;

0 0 1 -1 0 0 0 0 0 0 0 0;

0 0 1 0 -1 0 0 0 0 0 0 0;

0 0 0 0 0 1 -1 0 0 0 0 0;

0 0 1 0 0 0 0 0 0 0 0 0;

0 0 0 0 0 1 0 0 0 0 0 0;

0 0 0 0 0 0 1 0 0 0 0 0;

0 0 0 0 0 0 0 1 0 0 0 0;

0 0 0 0 0 0 0 0 0 0 0 1]

beq = zeros(9,1)

so the first four rows enforce your constraints where one element has to equal another and the other 5 rows force elements of x to be equal to zero.

I would suggest though that it would be better to reformulate your problem to not have these zeros included, since you already know what there value is. In this case you would only need a length seven vector x and you could have

Aeq = [1 -1 0 0 0 0 0;

0 0 1 -1 0 0 0;

0 0 1 0 -1 0 0;

0 0 0 0 0 1 -1 ]

beq = zeros(4,1)

##### 1 Comment

Jon
on 15 Apr 2019

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!