2

I want to solve a system of equations in c++. Is there any tool/package that provides a solver? My system looks like

(x-a)^2 + (y-b)^2 = d1 
(x-c)^2 + (y-d)^2 = d2

In that case I know a,..,d, d1,d2.

For know i took a spacial case (a,b,d = 0, and c not 0) but I want a solution in all cases.

Anybody an idea?

paul-g
  • 3,797
  • 2
  • 21
  • 36
ev die
  • 49
  • 1
  • 4
  • `blas` or `lapack` ... – malat Sep 29 '16 at 10:12
  • In c++ ^ is a binary operator, not squaring-a-number operator. – Stack Danny Sep 29 '16 at 10:22
  • @StackDanny `^` can be overloaded (although it does have low precedence); he's using braces for order of operation, though he would need to wrap so it looks more like `((x-a)^2) + ((y-b)^2) = d1 `. – George Sep 29 '16 at 10:25
  • I think the OP meant the power, ie. (x-a)*(x-a). So it's basically two circles of sqrt(d1) and sqrt(d2) radius, and centers in [a,b] and [c,d]. The solution exists when the circles do touch (single point) or cross (two points) (and 0 < d1/d2). I wonder why anyone would want an external library for this, if you would exercise it a bit on paper, I'm sure you would come with few simple formulas to calculate [x,y] pairs. – Ped7g Sep 29 '16 at 10:54
  • the solution is a perpendicular bisector, you can derive it by hand. – user1095108 Sep 29 '16 at 11:09

4 Answers4

3

If you need general support for solving nonlinear equations Ceres, PetSC, dlib all have nonlinear solvers that you can use from C++ to solve the problems you describe. Though you are much more likely to find better support for this type of work in Matlab or even python's scipy. Particularly if you are not really interested in performance, and only need to solve small scale equations with ease.

If all you need is to solve the system you posted, there is a simple closed form solution:

  1. Subtract eq2 from eq1 and express x = f(y) [s1]
  2. Substitute x with f(y) in one of the equations and solve for y
  3. Substitute y back in [s1] to find x
paul-g
  • 3,797
  • 2
  • 21
  • 36
2

I suggest you read 'Numerical Recipes'. This book has a chapter on equations solving, and their preface usually gives a very good overview in simple enough terms on all the subject. please note, that solving equations numerically has many fine details, and using any package without handling the details may lead to a bad solution (or maybe just slow, or not good enough).

Hezi
  • 21
  • 2
2

In geometrical sense, the system of equations(SOE) represent two circles. The first one a circle whose center is at (a,b) and of raduis sqrt(d1), and the second one a circle at (c,d) with radius of sqrt(d2).

There are three cases to consider

the first case is if the two circles do not
intersect. In this case the equation does not have a 
solution.

The second case is if the two circles intersect at 
two points. In such case the equations will have two 
solutions. i.e two possible values for (x,y)

In third case the two circles intersect at exactly 
two points. In this case the SOE has exactly one 
solution. i.e one pair of solution (x,y).

So how do we check if the SOE, has a solution. well we check if the two circles intersect. The two circles intersect iff: The distance between the two circles is less than or equal to the sum of their radii.

sqrt( (a-c)^2 + (b-d)^2 ) <= sqrt(d1) + sqrt(d2).

if the equality holds then the two circles intersect in exactly one point and therfore the SOE has exactly one solution.

I can continue explaining but I will leave you with the equation. Check this out:

https://math.stackexchange.com/questions/256100/how-can-i-find-the-points-at-which-two-circles-intersect#256123

PyHomer
  • 79
  • 6
0

Yes, this one supports nonlinear systems and overloaded ^ operator.

Here is an example: https://github.com/ohhmm/NonLinearSystem

Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54