Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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

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.

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

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

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top