# GAMS Support Wiki

### Site Tools

gams:using_gams_for_solving_square_nonlinear_systems

# Using GAMS for solving Square Nonlinear Systems

Q: How can I use GAMS to solve a system of non-linear equations, like: `fi(Xi) = 1` where: fi’s are non-linear functions of xi’s and `i = 1,…, n`.

Contributed by Tom Rutherford to the GAMS-User List: There are three basic approaches available for solving square nonlinear systems under GAMS:

1) Formulate as a NLP (nonlinear program) with a irrelevant objective function:

`    max anything s.t. f_i(x) = 0 i=1,...,n`

2) Formulate as an MCP (mixed compelementarity problem) without bounds:

```    f_i(x) = 0 i=1,...,n''
-inf <= x_i <= +inf i=1,...,n```

3) Formulate as a CNS (constrained nonlinear system):

```   f_i(x) = 0 i=1,...,n
xlo_i <= x_i <= xup_i ```

Approaches (1) and (3) offer some advantages if the functions you are using are undefined for some values of x. You can then apply upper and lower bounds which assure that the algorithm does not wander off, but even with bounds you may not be assured of finding solution if the functions are not nicely behaved (monotone, P, etc.).

```\$TITLE THREE METHODS FOR SOLVING NONLINEAR SYSTEMS WITH GAMS
* VERY SIMPLE NONLINEAR SYSTEM:
SET I /I1*I10/; ALIAS (I,J);
PARAMETER SOLUTION SOLUTIONS FROM ALTERNATIVE FORMULATIONS;
C(I,J) LINEAR PARAMETER
B(I) INTERCEPT PARAMETER;
B(I) = UNIFORM(0,1);
C(I,J) = UNIFORM(0,1);
A(I) = UNIFORM(0,1);
VARIABLES X(I) UNKNOWN VECTOR X;
* FUNCTION F DEFINES THE SYSTEM OF EQUATIONS WHICH
* APPLY IN ALL FORMULATIONS:
EQUATIONS F(I) CONSTRAINTS ON X;
F(I)..
SUM(J, C(I,J) * X(J) + A(J) * X(J) * X(J)) - B(I) =E= 0;
* (1) FORMULATION AS A CONSTRAINED NONLINEAR SYSTEM:
MODEL CNS_NLSYS /F/;
X.L(I) = 1;
SOLVE CNS_NLSYS USING CNS;
SOLUTION(I,"CNS") = X.L(I);
* (2) FORMULATION AS AN MIXED COMPLEMENTARITY PROBLEM:
MODEL MCP_NLSYS /F.X/;
X.L(I) = 1;
OPTION MCP=MILES;
SOLVE MCP_NLSYS USING MCP;
SOLUTION(I,"MCP") = X.L(I);
* (3) FORMULATION AS A NONLINEAR PROGRAM:
VARIABLE OBJ DUMMY OBJECTIVE;
EQUATION OBJDEF DEFINES THE DUMMY OBJECTIVE;
OBJDEF.. OBJ =E= 1;
X.L(I) = 1;
MODEL NLP_NLSYS /OBJDEF, F/;
SOLVE NLP_NLSYS USING NLP MAXIMIZING OBJ;
SOLUTION(I,"NLP") = X.L(I);
* PRINT OUT A COMPARISON:
OPTION SOLUTION:8;
DISPLAY SOLUTION;
\$ontext
The program should produce:
----     38 PARAMETER SOLUTION  SOLUTIONS FROM ALTERNATIVE FORMULATIONS
CNS         MCP         NLP
I1  -0.82541120 -0.82541120 -0.82541120
I2   0.42506470  0.42506470  0.42506471
I3  -0.29439840 -0.29439840 -0.29439840
I4  -0.46446261 -0.46446261 -0.46446260
I5  -0.58034820 -0.58034820 -0.58034819
I6   0.16156815  0.16156815  0.16156813
I7  -1.00001459 -1.00001459 -1.00001461
I8   0.11500797  0.11500797  0.11500797
I9   0.44654506  0.44654506  0.44654506
I10  0.64782840  0.64782840  0.64782839
\$offtext``` 