Skip to main content

solve system of nonlinear equations+Matlab

Answered

Comments

6 comments

  • Eli Towle
    Gurobi Staff Gurobi Staff

    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
  • Gary Amin
    Gurobi-versary
    First Question
    First Comment

    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
  • Eli Towle
    Gurobi Staff Gurobi Staff

    Hi Amin,

    Great, I'm glad that helped!

    The \( q \) field is the vector defining the linear term on the left-hand 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
  • Gary Amin
    Gurobi-versary
    First Question
    First Comment

    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
  • Eli Towle
    Gurobi Staff Gurobi Staff

    Gurobi supports (convex or non-convex) 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 non-convex 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
  • Gary Amin
    Gurobi-versary
    First Question
    First Comment

    Thank you so much.

    In theory, it should work.

    0

Please sign in to leave a comment.