# C/C++ library for solving nonlinear systems of equations

My system of equations is like this:

(x - a1)2 + (y - b1)2 = c1

(x - a2)2 + (y - b2)2 = c2

I know it is simple using matlab:

``````solve((x-a1)^2 + (y-b1)^2 - c1, (x-a2)^2 + (y-b2)^2 - c2)
``````

But how to solve this problem using C/C++? I know a math library called `lapack`, but is for linear equation. Any suggestions?

• I wrote one implementation of Newton-Raphson method, see here for details: dilawarnotes.wordpress.com/2016/04/14/… . It is not well tested. – Dilawar Apr 16 '16 at 6:39
• there's no language called C/C++. C and C++ are different languages – phuclv Sep 2 '18 at 14:33

I'm assuming you mean free software. Referencing the above stackoverflow question, you could use:

The rest of them that are listed on the referenced stackoverflow question are Fortran based. You can only use them if you use f2c (Fortran to C program)

I would like suggest few answers I found from the web after doing a bit of search.but Its always the fact that library depends upon your individual needs :)

• Eigen Eigen is a C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms. It supports all matrix sizes, from small fixed-size matrices to arbitrarily large dense matrices, and even sparse matrices.and supports all standard numeric types, including std::complex, integers, and is easily extensible to custom numeric types.various matrix decompositions and geometry features.Its ecosystem of unsupported modules provides many specialized features such as non-linear optimization, matrix functions, a polynomial solver, FFT, and much more.
• Trilinos It provides a lot of classes and functions to manage vectors and matrices in parallel, to solve linear and non-linear systems, to solve ordinary differential equations and calculate eigenvalues, etc.
• ALIAS-C++ A C++ Algorithms Library of Interval Analysis for equation Systems for Solving systems with linear and non-linear terms

• MINPACK It is a library of FORTRAN subroutines for the solving of systems of nonlinear equations, or the least squares minimization of the residual of a set of linear or nonlinear equations.

Source : List of numerical libraries

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

``````    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:

((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;
``````