How do I use GAMS to solve a system of non-linear equations, like: *fi(xi) = 1* where: *fi* are non-linear functions of *xi* and *i = 1,…,n*.

There are three basic approaches available for solving square nonlinear systems under GAMS:

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

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

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

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

- 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(i,*) 'solutions from alternative formulations'
a(i) 'quadratic parameter'
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'
obj 'dummy objective';
* function f defines the system of equations which apply in all formulations:
Equations
f(i) 'constraints on x'
objdef 'defines the dummy objective';
f(i).. sum(j, c(i,j) * x(j) + a(j) * x(j) * x(j)) - b(i) =e= 0;
objdef.. obj =e= 1;
* (1) formulation as a nonlinear program:
model nlp_nlsys /objdef, f/;
x.l(i) = 1; solve nlp_nlsys using nlp maximizing obj; solution(i,"nlp") = x.l(i);
* (2) formulation as an mixed complementarity problem:
model mcp_nlsys /f.x/;
x.l(i) = 1; solve mcp_nlsys using mcp; solution(i,"mcp") = x.l(i);
* (3) 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);
* print out a comparison:
option solution:8; display solution;
$onText
the program should produce:
---- 35 PARAMETER solution solutions from alternative formulations
nlp mcp cns
i1 -0.82541120 -0.82541120 -0.82541120
i2 0.42506471 0.42506470 0.42506470
i3 -0.29439840 -0.29439840 -0.29439840
i4 -0.46446260 -0.46446261 -0.46446261
i5 -0.58034819 -0.58034820 -0.58034820
i6 0.16156813 0.16156815 0.16156815
i7 -1.00001461 -1.00001459 -1.00001459
i8 0.11500797 0.11500797 0.11500797
i9 0.44654506 0.44654506 0.44654506
i10 0.64782839 0.64782840 0.64782840
$offText
```