我想用java来帮助我快速解决方程,以便在我正在进行的三边测量项目中得到x和y。
我明白了我需要改变的唯一数字是那些我被标记为(a)和(b)的数字
120x - 15600 + 40y - 4400 = 1200(a)
y = -3x + 530
-180x + 20700 + 60y - 8100 = -2400(b)
y = 3x - 250
所以我继续解决并得到答案
-3x + 530 = 3x - 250
780 = 6x
130 = x
y = 3(130) - 250
y = 140
x= 130, y = 140
我只需要java来计算等式,然后就可以改变两个值(a)和(b)
所以我想要一点帮助,看看我能不能做到这一点?有人可以帮帮我吗?我不是一个伟大的程序员,因为它不是我的领域
答案 0 :(得分:1)
这只是一般数学:
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)
在你的情况下:
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
无论如何,我想离题,你想要:
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
使用您的号码验证公式:
y = b / 120 + a / 80 + 145
= -2400 / 120 + 1200 / 80 + 145
= 140
x = (a / 40 - y + 500) / 3
= (1200 / 40 - 140 + 500) / 3
= 130
或者在Java代码中:
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);
答案 1 :(得分:0)
你可以重组(a)和(b),使常数全部在等式的右边
120x + 40y = 21200(a)
-180x + 60y = -15000(b)
设为120的系数x,40的系数为y,21200为等式a的常数。方程式b也类似。并使用这些名称作为程序的变量。
在二元二次方程中求解两个未知变量的一种方法是通过操纵两个方程中的系数和常数来消除一个未知变量,并将它们组合起来去除一个未知变量,这已在您的问题中显示
如您所示,您首先消除x然后求解y。在下面的程序中,我先消除y然后求解x。但基本上,它们是相同的。
注意程序假定系数和常数是整数。
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;
}
}