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?


3 Answers 3


Reference: What good libraries are there for solving a system of non-linear equations in C++?

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

  1. Sundials: https://computation.llnl.gov/casc/sundials/main.html
  2. GNU: http://www.gnu.org/software/gsl/manual/html_node/Multidimensional-Root_002dFinding.html#index-nonlinear-systems-of-equations_002c-solution-of-2426

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;

Here is the sample CMake project: https://github.com/ohhmm/NonLinearSystem

  • 1
    do you have a Linux version? Just wanna quickly test your codes. No windows here:(
    – math101
    Commented Apr 8, 2020 at 17:36
  • Yes, it is crossplatform with cmake. If you face difficulties with compiling then try to set clang compiler while configuring. Fill free to send pull requests. Current dev branch is 'ts'. Commented Apr 8, 2020 at 17:57
  • @math101, deduction-fw.org Commented Apr 8, 2020 at 17:58
  • do I really need clang? I think clang is mostly for mac users. I use VS codes and ::codeblock for cpp on linux...dunt think i have a clang compiler here:(
    – math101
    Commented Apr 8, 2020 at 19:01
  • im on ubuntu 20.04. Perhaps i should get clang-9?
    – math101
    Commented Apr 8, 2020 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.