10

In a C++ application I'm coding, I need to solve a system of non-linear equations (N equations, N unknowns).

The systems I'm solving will be rather small (up to 10 equations/unknowns), so performance is not going to be a real issue. I've searched the web a bit for a non-linear solver library, and I couldn't get to something which looks easy to use (got to NOX and C/C++ Minpack, but both seem to be an overkill for my need).

Any thoughts and ideas of easy-to-use libraries for this purpose?

hoffer
  • 649
  • 2
  • 7
  • 11

8 Answers8

3

One thing should be clear: non-linear equation solution isn't easy. It's not the same as solving linear equations. You aren't always guaranteed to get a solution. And your choice of initial condition and incrementation strategy can have a profound effect on the solution you do get.

With that said, I can't recommend a particular library, but you should be on the lookout for a linear algebra package that includes Newton-Raphson iteration in its menu of choices.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

There are two options for you, you can use the sundials packages which includes a nonlinear solver, written in C I think. The only problem I've found with it is that you need to give it good initial estimates. The second option is to use NLEQ or NLEQ2 which I think are superior (writtein in FORTRAN but easy to link to C like langages. However I have had some problems locating it just now. There is a good web site with a list of possible options at: http://plato.asu.edu/sub/zero.html

rhody
  • 2,274
  • 2
  • 22
  • 40
  • Thanks. I've used the sundials package (KINSOL) and it works like a charm from a C++ program on Windows 64. – hoffer Nov 21 '10 at 19:35
  • hi @hoffer, I am also very interested in solving nonlinear systems by Newton-type method. About the usage of NLEQ and NLEQ2, do you have any simple working example to show the implementations of the algorithm? Thanks – LCFactorization Nov 10 '13 at 05:56
  • hi @rhody, how can I link NLEQ/NLEQ2 in Fortran to C++? I have visual C++ 9.0 Express in Win32 OS. Do I need any Fortran77 complier? thanks – LCFactorization Nov 10 '13 at 06:01
  • You'll need a fortran compiler, probably g77 will work (I happened to use a commercial one). After than you'll need to access the Fortran through a C call not C++. – rhody Nov 14 '13 at 21:12
1

Numerical Recipes has a routine that will do the job for you.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • A great source of code is netlib. Mostly it's FORTRAN but f2c will soon see to that. Incidentally I'd say f2c is one of the great programs of all time! I'd recommend Numerical Recipes as a great way to get good orientation in many areas of numerical analysis without needing to be an expert. – David Heffernan Nov 20 '10 at 18:17
1

It depends on how non-linear the equations are. If they possess some "nice" properties...most obvious being positive-semi-definite matrix or convexity, there may be specialized algorithms available. I use IBM/ILOG CPLEX for most of my linear programming needs. Libraries are provided that can be pulled into C++ applications. Although I have not used their quadratic programming module, it is really the state-of-the-art in high horse-power linear and (well-behaved) non-linear programming.

Tryer
  • 3,580
  • 1
  • 26
  • 49
1

There is always GSL, but all the comments made in the other answers apply to this as well:

http://www.gnu.org/software/gsl/manual/html_node/Multidimensional-Root_002dFinding.html#index-nonlinear-systems-of-equations_002c-solution-of-2426

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • GSL Ch 38. [Non Linear Least-Squares Fitting](http://www.gnu.org/software/gsl/manual/html_node/Nonlinear-Least_002dSquares-Fitting.html#Nonlinear-Least_002dSquares-Fitting) – Mark Mikofski Sep 17 '13 at 17:52
0

Have you looked at COIN-OR? It might help if you submit your question to the OR-Exchange.

adamo
  • 3,584
  • 1
  • 18
  • 23
0

It's not free by any means, but Solver would work here.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
0

Microsoft Z3 https://github.com/Z3Prover/z3/blob/master/examples/c%2B%2B/example.cpp

also consider omnn::math: https://github.com/ohhmm/openmind/blob/master/omnn/math/test/08_System.cpp

Lets say system of equations is like this:

(x-a1)^2 + (y-b1)^2 = c1
(x-a2)^2 + (y-b2)^2 = c2

Then you have couple options:

Valuable a1, a2, b1, b2; // init with values

System sys;
Variable x,y;
sys << (x-a1)^2 + (y-b1)^2 - c1; // addin an equation as an equality to 0
sys << (x-a2)^2 + (y-b2)^2 - c2;

for(auto& solution : sys.Solve(x))
        std::cout << solution;

alternative way is to make single equation (see why):

((x-a1)^2 + (y-b1)^2 - c1)^2 + ((x-a2)^2 + (y-b2)^2 - c2)^2 = 0

Variable x,y;
Valuable a1, a2, b1, b2; // init with values
auto eq = ((x-a1)^2 + (y-b1)^2 - c1)^2 + ((x-a2)^2 + (y-b2)^2 - c2)^2;
eq.SetView(Valuable::View::Equation);  // optional: equation optimizations
// get y function:
auto fn = eq(y);

// show
std::cout << fn << std::endl;

// evaluate
auto evaluate = fn;
evaluate.eval(x, 10);
evaluate.optimize(); // calculate
// show calculated value at x=10:
std::cout << evaluate << std::endl;
Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54