简体   繁体   中英

Using java to solve an equation

I'm wanting to use java to help me quickly solve an equation to get x and y on a trilateration project i'm working on.

I get tot his point where the only numbers that i need to change is the ones i'be labelled (a) and (b)

120x - 15600 + 40y - 4400 = 1200(a)  

y = -3x + 530

-180x + 20700 + 60y - 8100 = -2400(b)  

y = 3x - 250

so i continue to solve and will get the answer

-3x + 530 = 3x - 250

780 = 6x

130 = x

y = 3(130) - 250

y = 140


x= 130, y = 140

I just need java to work out the equation and then be able to change the two values (a) and (b)

so im asking for a little bit of help as to whether or not i can do this? and could someone help me out please? im not a great programmer as its not my field

This is just general math:

Ax + By = C
Dx + Ey = F

// From first formula
x = (C - By) / A

// Applied to second formula
D * ((C - By) / A) + Ey = F
D * C / A - D * By / A + Ey = F
D * C / A + (E - D * B / A) * y = F
y = (F - D * C / A) / (E - D * B / A)
  = (A * F - D * C) / (A * E - D * B)

In your case:

 120x - 15600 + 40y - 4400 = 1200 
-180x + 20700 + 60y - 8100 = -2400

A = 120    B = 40   C = 1200 + 15600 + 4400  = 21200
D = -180   E = 60   F = -2400 - 20700 + 8100 = -15000

y = (A * F - D * C) / (A * E - D * B)
  = (120 * -15000 - -180 * 21200) / (120 * 60 - -180 * 40)
  = 2016000 / 14400
  = 140
x = (C - By) / A
  = (21200 - 40 * 140) / 120
  = 15600 / 120
  = 130

Anyway, I digress, you want:

 120x - 15600 + 40y - 4400 = a
-180x + 20700 + 60y - 8100 = b

// Normalized
(A= 120)x + (B=40)y = (C=a + 15600 + 4400)
(D=-180)x + (E=60)y = (F=b - 20700 + 8100)

C = a + 15600 + 4400 = a + 20000
F = b - 20700 + 8100 = b - 12600

y = (A * F - D * C) / (A * E - D * B)
  = (120 * (b - 12600) - -180 * (a + 20000)) / (120 * 60 - -180 * 40)
  = ((120 * b - 120 * 12600) - (-180 * a + -180 * 20000)) / 14400
  = (120 * b - 1512000 + 180 * a + 3600000) / 14400
  = 120 * b / 14400 + 180 * a / 14400 + (3600000 - 1512000) / 14400
  = b / 120 + a / 80 + 145
x = (C - By) / A
  = (a + 20000 - 40 * y) / 120
  = a / 120 + 20000 / 120 - 40 * y / 120
  = (a / 40 - y + 500) / 3

Verifying formulas with your numbers:

y = b / 120 + a / 80 + 145
  = -2400 / 120 + 1200 / 80 + 145
  = 140
x = (a / 40 - y + 500) / 3
  = (1200 / 40 - 140 + 500) / 3
  = 130

Or in Java code:

double a = 1200;
double b = -2400;
double y = b / 120 + a / 80 + 145;
double x = (a / 40 - y + 500) / 3;
System.out.println("x = " + x + ", y = " + y);

IDEONE

You can reorganize (a) and (b) so that the constants are all on the right side of the equation

120x + 40y = 21200(a)  

-180x + 60y = -15000(b)  

Let's name 120 the coefficient of x, 40 the coefficient of y and 21200 as the constant for equation a. The similar goes for equation b. And use these names as variables for the program.

One way to solve the two unknown variables in a binary quadric equation is to eliminate one unknown variables by manipulate the coefficient and constants in the two equations and combine them to get rid of one unknown variable, which you have shown in your question.

As shown by you, you eliminated x first and then solve for y. In the following program, I eliminated y first and then solve for x. But essentially, they are the same.

Note the program assumes that the coefficients and constants are integers.

public class Equation {
public static void main(String[] args) {
    int coefficient_x_1, coefficient_y_1, constant_1, coefficient_x_2, coefficient_y_2, constant_2;

    Scanner in = new Scanner(System.in);

    // 1. get the inputs
    coefficient_x_1 = in.nextInt();
    coefficient_y_1 = in.nextInt();
    constant_1 = in.nextInt();
    coefficient_x_2 = in.nextInt();
    coefficient_y_2 = in.nextInt();
    constant_2 = in.nextInt();

    // 2. try to eliminate x from the two equations to get the value of y
    // 2.1 get the least common multiplier of the two coefficient of x in the two equations
    int leastCommonMultiplier =
            Math.abs(coefficient_x_1) * Math.abs(coefficient_x_2) / getMaxCommonFactor(Math.abs(coefficient_x_1), Math.abs(coefficient_x_2));

    int cancellationFactor = -1;
    if (coefficient_x_1 * coefficient_y_1 < 0) cancellationFactor = 1;

    int multiplier_1 = leastCommonMultiplier / coefficient_x_1;
    int multiplier_2 = cancellationFactor * leastCommonMultiplier / coefficient_x_2;
    // 2.2 eleminate x and solve for y
    int y = (constant_1 * multiplier_1 + constant_2 * multiplier_2) / (coefficient_y_1 * multiplier_1 + coefficient_y_2 * multiplier_2);

    // 3. get the value of x based on the value of y
    int x = (constant_1 - coefficient_y_1 * y) / coefficient_x_1;

    System.out.println("x is : " + x);
    System.out.println("y is : " + y);

    in.close();
}

public static int getMaxCommonFactor(int a, int b) {
    if (a < b) {
        int temp = a;
        a = b;
        b = temp;
    }

    while (a % b != 0) {
        int temp = a % b;
        a = b;
        b = temp;
    }

    return b;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM