solve system of nonlinear equations+Matlab
AnsweredHello, Good day. I am a newbie in Gurobie and I would be grateful if you can help me with this question that I try to sole in Matlab.
I have a nonlinear system of equations and a constraint to minimize each variable:
There are 12 equations and 17 variables{x(1),x(2),...,x(17)}.
If variables x(8),...,x(17) are constant numbers, I can use the same approach in "bilinear.m" example and solve it. But for this case, I cannot find a solution.
Thank you in advance,
Amin

Hi Amin,
From the documentation, you need to additionally define the linear term for each of the quadratic constraints. This can be done by modifying the m.quadcon(i).q field.
Some sample code to solve your nonlinear system is below. It doesn't include an objective function.
% Define data
qrow = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3];
qcol = [4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7];
qval = ones(12);
lcol = [8, 9, 10, 11, 12, 13, 1, 1, 14, 15, 16, 17];
lval = [4, 4, 4, 4, 5, 5, 0, 0, 7, 7, 7, 7];
rhs = [0, 0, 0, 0, 0, 0, 25, 15, 0, 0, 0, 0];
n = 17;
% Add quadratic constraints
for i=1:length(qrow)
disp(i);
m.quadcon(i).Qrow = qrow(i);
m.quadcon(i).Qcol = qcol(i);
m.quadcon(i).Qval = qval(i);
m.quadcon(i).q = sparse(lcol(i), 1, lval(i), n, 1);
m.quadcon(i).rhs = rhs(i);
m.quadcon(i).sense = '=';
m.quadcon(i).name = sprintf('qcon%d', i);
end
% Add variable names
vnames = cell(n,1);
for i=1:n
vnames{i} = sprintf('x%d', i);
end
m.varnames = vnames;
% No linear constraints
m.A = sparse(0,n);
% Solve model and display solution
params.NonConvex = 2;
result = gurobi(m, params);
disp(result.x);I hope this helps!
Eli
2 
Thank you so much, Eli.
You solved my problem!
I still do not understand this line:
m.quadcon(i).q = sparse(lcol(i), 1, lval(i), n, 1);
Have a nice day!
Amin
1 
Hi Amin,
Great, I'm glad that helped!
The \( q \) field is the vector defining the linear term on the lefthand side of the quadratic constraint. For example, the linear term of the first constraint is \( 4 x_8 \), which we can write as \( q^\top x \), where the 8th element of \( q \) is \( 4 \), and all other elements of \( q \) are zero. That line of code defines this vector in sparse form for each constraint; see the MATLAB documentation for more details about the "sparse" function.
Eli
0 
Dear Eli,
Good day.
I have a similar question. I want to solve a 'multilinear' system of equations with Gurobi.
For example:
a*b*c = f_1
d*e = f_2
f*b*g*h= f_3
So, every equation is the product of 2 or more than 2 variables.
(f_1,f_2 and f_3 are constant numbers, and {a,b,c,d,e,f,g,h} are the variables)
Is there any way to do it in Gurobi?
Thank you in advance!
Amin
0 
Gurobi supports (convex or nonconvex) quadratic constraints, like the constraint \( x_1 x_4 = 4 x_8 \) from your original post. If you want to model more general multilinear expressions, you can do this by introducing additional auxiliary variables. For example, you can model
$$\begin{align*}x_1 x_2 x_3 = z\end{align*}$$
by introducing an auxiliary variable \( u \) and adding the following two bilinear constraints:
$$\begin{align*} x_1 x_2 &= u \\ u x_3 &= z.\end{align*}$$
Of course, adding more nonconvex quadratic constraints results in a model that is harder to solve. Additionally, the solver can encounter numerical problems when you "chain" together multiple bilinear constraints like this.
1 
Thank you so much.
In theory, it should work.
0
Please sign in to leave a comment.
Comments
6 comments