-2

I'm looking for some assistance with solving the below equations in Java

(a-x1)^2 + (b-y1)^2 = r1^2 + r^2
(a-x2)^2 + (b-y2)^2 = r2^2 + r^2
(a-x3)^2 + (b-y3)^2 = r3^2 + r^2

Values of x1, y1, r1, x2, y2, r2 & x3, y3, r3 are known. I need to solve for a, b, r

How to go about doing this in Java? I checked the Commons Maths library but didn't find how I could achieve this. It helps with linear equations though.

Pavan Kulkarni
  • 456
  • 3
  • 15

2 Answers2

3

I think you need linear equations for Gaussian elimination.

If a, b, and r are what you need to solve for, it's obvious that these are non-linear equations.

You'll need a non-linear solver, like Newton-Raphson.

You'll have to linearize your equations. Calculate the Jacobean for the differentials da, db, and dr.

You'll start with an initial guess

a = a(old)
b = b(old)
r = r(old)

use a linearized version of the equations to calculate an increment

2*(a(old)-x1)*da + 2*(b(old)-y1)*db = 2*r(old)*dr
2*(a(old)-x2)*da + 2*(b(old)-y2)*db = 2*r(old)*dr
2*(a(old)-x3)*da + 2*(b(old)-y3)*db = 2*r(old)*dr

update your guess

a(new) = a(old) + da
b(new) = b(old) + db
r(new) = r(old) + dr

and repeat until it converges (if it converges).

You should never solve linear equations using Gaussian elimination: it suffers from a number of problems. A better idea is to do LU decomposition and forward-back substitution.

If my linearized equations are correct, they take the form A(dx) = 0. What should the boundary condition be?

(a, b) are the coordinates for the center of the circle; r is the radius.

Do you really have three points (x1, y1), (x2, y2), and (x3, y3)? Or do you have lots more points? If it's the latter, you'll need a least squares fit.

duffymo
  • 293,097
  • 41
  • 348
  • 541
  • +1 Gaussian elimination definitely won't work. This is more of a mathematics / numerical computation problem than a programming problem. Most of the SO readership probably never did or have forgotten how to do the relevant maths. (Myself included!) – Stephen C Sep 12 '12 at 11:34
0

hope this method can give you some ideas:

public int[] getCoordinates(float XR_1, float YR_1, float XR_2, float YR_2,
                             float XR_3, float YR_3, int R1, int R2, int R3) {
    //define the positions
    int XU_1 = 0, YU_1 = 0, XU_2 = 0, YU_2 = 0, XU, YU;
    //define variables and arrays that needed
    float D0[][] = new float[17][50];
    float D1[][] = new float[17][50];
    float f[][] = new float[17][50];
    float fmin_1 = 0;
    float fmin_2 = 0;
    //define columns and rows
    int i, j;
    //Y goes from 0 to 49
    for(j=0; j<=49; j++){
        //X goes from 0 to 16
        for(i=0; i<=16; i++){
            D0[i][j] = (float) (Math.pow((i-XR_1),2) + Math.pow((j-YR_1),2) - Math.pow(R1,2));
            D1[i][j] = (float) (Math.pow((i-XR_2),2) + Math.pow((j-YR_2),2) - Math.pow(R2,2));
            f[i][j] = (float) Math.sqrt(Math.pow(D0[i][j], 2) + Math.pow(D1[i][j], 2));
            //get two position where f[i][j] are the minimum
            //initialise the minimum two positions
            if(i==0 & j==0){
                fmin_1 = f[i][j];
                XU_1 = i;
                YU_1 = j;
            }
            else if(j==0 & i==1){
                if(f[i][j] < fmin_1){
                    fmin_2 = fmin_1;
                    fmin_1 = f[i][j];
                    XU_2 = XU_1;
                    XU_1 = i;
                    YU_2 = YU_1;
                    YU_1 = j;
                }
                else {
                    fmin_2 = f[i][j];
                    XU_2 = i;
                    YU_2 = j;
                }
            }
            else{
                if(f[i][j] < fmin_1){
                    fmin_2 = fmin_1;
                    fmin_1 = f[i][j];
                    XU_2 = XU_1;
                    XU_1 = i;
                    YU_2 = YU_1;
                    YU_1 = j;
                }
                else if(f[i][j] < fmin_2){
                    fmin_2 = f[i][j];
                    XU_2 = i;
                    YU_2 = j;
                }
            }
        }
    }

this method gives two closest points in the coordinate system, you can use the similar way to get the most ideal one.

M.You
  • 1
  • 1