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;
}
}
Related
I am in the process of taking over a project and noticed that, when the vincenty formulae is used,
it has been written in a unusual way. This is how it's written:
String direct(double distance, double initialBearing, double positionlat, double positionlong) {
// if (this.height != 0) throw new RangeError('point must be on the surface of
// the ellipsoid');
double φ1 = this.toRad(positionlat)/* .toRadians() */, λ1 = this.toRad(positionlong)/* .toRadians() */;
double α1 = this.toRad(initialBearing);
double s = distance;
// allow alternative ellipsoid to be specified
// double ellipsoid = /*this.datum ? this.datum.ellipsoid :*/
// LatLonEllipsoidal.ellipsoids.WGS84;
// const {a, b, f} = ellipsoid;
double a = 6378137;
double b = 6356752.314245;
double f = 1 / 298.257223563;
// double a = ellipsoid;
// double b = ellipsoid;
// double f = ellipsoid;
double sinα1 = Math.sin(α1);
double cosα1 = Math.cos(α1);
double tanU1 = (1 - f) * Math.tan(φ1), cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1)), sinU1 = tanU1 * cosU1;
double σ1 = Math.atan2(tanU1, cosα1); // σ1 = angular distance on the sphere from the equator to P1
double sinα = cosU1 * sinα1; // α = azimuth of the geodesic at the equator
double cosSqα = 1 - sinα * sinα;
double uSq = cosSqα * (a * a - b * b) / (b * b);
double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
double σ = s / (b * A);
Double sinσ = null, cosσ = null, Δσ = null; // σ = angular distance P� P₂ on the sphere
Double cos2σₘ = null; // σₘ = angular distance on the sphere from the equator to the midpoint of the
// line
Double σʹ = null, iterations = 0d;
do {
cos2σₘ = Math.cos(2 * σ1 + σ);
sinσ = Math.sin(σ);
cosσ = Math.cos(σ);
Δσ = B * sinσ * (cos2σₘ + B / 4 * (cosσ * (-1 + 2 * cos2σₘ * cos2σₘ)
- B / 6 * cos2σₘ * (-3 + 4 * sinσ * sinσ) * (-3 + 4 * cos2σₘ * cos2σₘ)));
σʹ = σ;
σ = s / (b * A) + Δσ;
} while (Math.abs(σ - σʹ) > 1e-12 && ++iterations < 100);
if (iterations >= 100) {
//throw new Exception("Vincenty formula failed to converge"); // not possible?
System.err.println("Warning: Vincenty formula failed to converge!");
}
double x = sinU1 * sinσ - cosU1 * cosσ * cosα1;
double φ2 = Math.atan2(sinU1 * cosσ + cosU1 * sinσ * cosα1, (1 - f) * Math.sqrt(sinα * sinα + x * x));
double λ = Math.atan2(sinσ * sinα1, cosU1 * cosσ - sinU1 * sinσ * cosα1);
double C = f / 16 * cosSqα * (4 + f * (4 - 3 * cosSqα));
double L = λ - (1 - C) * f * sinα * (σ + C * sinσ * (cos2σₘ + C * cosσ * (-1 + 2 * cos2σₘ * cos2σₘ)));
double λ2 = λ1 + L;
double α2 = Math.atan2(sinα, -x);
// const destinationPoint = new
// LatLonEllipsoidal_Vincenty(this.toDeg(φ2)/*.toDegrees()*/,
// this.toDeg(λ2)/*.toDegrees()*/, 0, undefined);
return this.toDeg(φ2) + ";" + this.toDeg(λ2);/*
* { lat: this.toDeg(φ2), lng: this.toDeg(λ2)
*/
/*
* destinationPoint.g point: destinationPoint, finalBearing:
* Dms.wrap360(this.toDeg(α2)/*.toDegrees()
*//*
* ), iterations: iterations,
*/
// };
}
Now the IDE (eclipse) is responding, that it can't handle variable names such as e.g. φ1. Is there a elegant solution to fixing this or do I have to re-write it?
Most probably, the source code was written in the UTF-8 encoding, and your Eclipse is configured to interpret the sources as ANSI, ISO-8859-1, cp1252 or similar.
Then, what you see as φ is in fact the two-byte UTF-8 representation of the greek character phi (φ), interpreted according to ANSI.
Configure Eclipse to expect the UTF-8 encoding (Preferences / General / Workspace / Text file encoding), then it should be able to compile.
This example shows why it is a bad idea even today to use characters outside ASCII in source code.
How to write this theorem correctly as is written in the formula?
package com.company;
public class Exercise8 {
public static void main(String[] args) {
double AB = 6;
double AC = 16;
double Angle = 60;
double CosOfAngle = 0.5;
// Почему-то значение косинуса 60 градусов вместо 0.5, пишет
// -0.9524129804151563 ? ? ? (Do not pay attention)
// Formula is BC^2 = AB^2 + AC^2 - 2AB*AC * cos A
double bc = (2 * (Math.pow(AB, 2) + Math.pow(AC, 2) - ((AB * AC))) * CosOfAngle);
double BC = Math.sqrt(bc);
double P = AB + BC + AC;
double p = 0.5 * P; // Где p - полупериметр
double S0 = (p * ((p - AB) * (p - BC) * (p - AC)));
double S1 = Math.sqrt(S0);
double S = Math.round(S1);
System.out.println("Perimeter of triangle is : " + P + " cm ");
System.out.println("Area of triangle is : " + S + " cm^2 ");
}
}
The mistake is in this line:
double bc = (2 * (Math.pow(AB, 2) + Math.pow(AC, 2) - ((AB * AC))) * CosOfAngle);
which should be:
double bc = Math.pow(AB, 2) + Math.pow(AC, 2) - 2 * AB * AC * CosOfAngle;
You were multiplying the whole formula by 2, whereas only the cosine part needs to be multiplied by two. There were too many confusing parenthesis. Removing them made it a lot clearer.
This seems simple to me:
// https://www.mathsisfun.com/algebra/trig-cosine-law.html
public double lawOfCosines(double a, double b, double angleInRadians) {
return Math.sqrt(a*a + b*b - 2.0*a*b*Math.cos(angleInRadians));
}
I made a java implementation of the new perceptualy uniform color space JzAzBz. OSA publication is : https://www.osapublishing.org/oe/fulltext.cfm?uri=oe-25-13-15131&id=368272.
My java code is :
private double b = 1.15;
private double g = 0.66;
private double c1 = 3424 / Math.pow(2, 12);
private double c2 = 2413 / Math.pow(2, 7);
private double c3 = 2392 / Math.pow(2, 7);
private double n = 2610 / Math.pow(2, 14);
private double p = 1.7 * 2523 / Math.pow(2, 5);
private double d = -0.56;
private double d0 = 1.6295499532821566 * Math.pow(10, -11);
public void XYZToJab(double[] xyz, double[] jab) {
double[] XYZp = new double[3];
XYZp[0] = b * xyz[0] - ((b - 1) * xyz[2]);
XYZp[1] = g * xyz[1] - ((g - 1) * xyz[0]);
XYZp[2] = xyz[2];
double[] LMS = new double[3];
LMS[0] = 0.41478972 * XYZp[0] + 0.579999 * XYZp[1] + 0.0146480 * XYZp[2];
LMS[1] = -0.2015100 * XYZp[0] + 1.120649 * XYZp[1] + 0.0531008 * XYZp[2];
LMS[2] = -0.0166008 * XYZp[0] + 0.264800 * XYZp[1] + 0.6684799 * XYZp[2];
double[] LMSp = new double[3];
for (int i = 0; i < 3; i++) {
LMSp[i] = Math.pow((c1 + c2 * Math.pow((LMS[i] / 10000.0), n)) / (1 + c3 * Math.pow((LMS[i] / 10000.0), n)), p);
}
double[] Iab = new double[3];
Iab[0] = 0.5 * LMSp[0] + 0.5 * LMSp[1];
Iab[1] = 3.524000 * LMSp[0] - 4.066708 * LMSp[1] + 0.542708 * LMSp[2];
Iab[2] = 0.199076 * LMSp[0] + 1.096799 * LMSp[1] - 1.295875 * LMSp[2];
jab[0] = (((1 + d) * Iab[0]) / (1 + d * Iab[0])) - d0;
jab[1] = Iab[1];
jab[2] = Iab[2];
}
public void JabToXYZ(double[] jab, double[] xyz) {
double[] Iab = new double[3];
Iab[0] = (jab[0] + d0) / (1 + d - d * (jab[0] + d0));
Iab[1] = jab[1];
Iab[2] = jab[2];
double[] LMSp = new double[3];
LMSp[0] = 1.0 * Iab[0] + 0.13860504 * Iab[1] + 0.05804732 * Iab[2];
LMSp[1] = 1.0 * Iab[0] - 0.13860504 * Iab[1] - 0.05804732 * Iab[2];
LMSp[2] = 1.0 * Iab[0] - 0.09601924 * Iab[1] - 0.81189190 * Iab[2];
double[] LMS = new double[3];
for (int i = 0; i < 3; i++) {
LMS[i] = 10000 * Math.pow((c1 - Math.pow(LMSp[i], 1 / p)) / ((c3 * Math.pow(LMSp[i], 1 / p)) - c2), 1 / n);
}
double[] XYZp = new double[3];
XYZp[0] = 1.92422644 * LMS[0] - 1.00479231 * LMS[1] + 0.03765140 * LMS[2];
XYZp[1] = 0.35031676 * LMS[0] + 0.72648119 * LMS[1] - 0.06538442 * LMS[2];
XYZp[2] = -0.09098281 * LMS[0] - 0.31272829 * LMS[1] + 1.52276656 * LMS[2];
xyz[0] = (XYZp[0] + (b - 1) * XYZp[2]) / b;
xyz[1] = (XYZp[1] + (g - 1) * XYZp[0]) / g;
xyz[2] = XYZp[2];
}
When I test it running XYZToJab and then JabToXYZ I get a good precision for X and Z (delta order is E-9) but for Y I get a bad precision (delta order is 1-5%).
Is there anyone who can help me ?
The implementation is almost correct: The error lies in JabToXYZ where the prior to last line should be changed from
(XYZp[1] + (g - 1) * XYZp[0]) / g;
to
(XYZp[1] + (g - 1) * xyz[0]) / g;
However the fact that you are using rounded invert matrices to 6 decimal places in the JabToXYZ function will prevent you to get a clean inversion. You should try to compute the inverse at full double precision:
>>> import numpy as np
>>> np.set_printoptions(formatter={'float': '{:0.15f}'.format})
>>> import colour.models.jzazbz
>>> colour.models.jzazbz.JZAZBZ_IZAZBZ_TO_LMS_P_MATRIX
array([[1.000000000000000, 0.138605043271539, 0.058047316156119],
[1.000000000000000, -0.138605043271539, -0.058047316156119],
[1.000000000000000, -0.096019242026319, -0.811891896056039]])
>>> colour.models.jzazbz.JZAZBZ_LMS_TO_XYZ_MATRIX
array([[1.924226435787607, -1.004792312595365, 0.037651404030618],
[0.350316762094999, 0.726481193931655, -0.065384422948085],
[-0.090982810982848, -0.312728290523074, 1.522766561305260]])
I am in the process of converting a block of python code to java. Code determines sun and moon rise and set times.
95% of the code conversion is fine but I am stuck on converting the python array moonArray to java as its not making sense. Coding up the java version is a trival task and dont need help with that, just understanding what the python array is doing.
PYTHON CODE:
def riseSetString(self, dis, utcdis):
moonArray = self.risesetlist(dis, utcdis)
moonString = "moonrise time is " + moonArray[0]
moonString += "\nmoonset time is " + moonArray[1]
moonString += "\n{} iterations".format(moonArray[2])
print(moonString)
def risesetlist(self, dis, utcdis):
y = datetime.datetime.utcnow().year
m = datetime.datetime.utcnow().month
d = datetime.datetime.utcnow().day + dis
h = datetime.datetime.utcnow().hour
mins = datetime.datetime.utcnow().minute
second = datetime.datetime.utcnow().second
moonArray = self.calculate(y, m, d, h, mins, second)
msd = moonArray[8]
mra = moonArray[5]
mdecl = moonArray[6]
gmsto = moonArray[11]
hmm = radians(-0.583) - msd
numerator = (sin(hmm) - sin(radians(self.localLat)) * sin(mdecl))
divisor = (cos(radians(self.localLat)) * cos(mdecl))
mlha = numerator / divisor
mlha = acos(mlha) * 12 * 15.0 / (15.04107 * pi)
utcmoon = (degrees(mra) - gmsto * 15 - self.localLong) / 15
a = mlha
c = 1
ff = 0
while abs(c) > 0.000001:
y = datetime.datetime.utcnow().year
m = datetime.datetime.utcnow().month
d = datetime.datetime.utcnow().day + dis
h = utcmoon + a
moonArray = self.calculate(y, m, d, h, 0, 0)
msd = moonArray[8]
mra = moonArray[5]
mdecl = moonArray[6]
gmsto = moonArray[11]
hmm = radians(-0.583) - msd
numerator = (sin(hmm) - sin(radians(self.localLat)) * sin(mdecl))
divisor = (cos(radians(self.localLat)) * cos(mdecl))
mlha = numerator / divisor
mlha = acos(mlha) * 12 * 15.0 / (15.04107 * pi)
utcmoon = (degrees(mra) - gmsto * 15 - self.localLong) / 15
b = mlha
c = abs(b - a)
a = mlha
ff += 1
if ff >= 100:
break
moonset = mlha + utcmoon
c = 1
while abs(c) > 0.000001:
y = datetime.datetime.utcnow().year
m = datetime.datetime.utcnow().month
d = datetime.datetime.utcnow().day + dis
h = utcmoon - a
moonArray = self.calculate(y, m, d, h, 0, 0)
msd = moonArray[8]
mra = moonArray[5]
mdecl = moonArray[6]
gmsto = moonArray[11]
hmm = radians(-0.583) - msd
numerator = (sin(hmm) - sin(radians(self.localLat)) * sin(mdecl))
divisor = (cos(radians(self.localLat)) * cos(mdecl))
mlha = numerator / divisor
mlha = acos(mlha) * 12 * 15.0 / (15.04107 * pi)
utcmoon = (degrees(mra) - gmsto * 15 - self.localLong) / 15
b = mlha
c = b - a
a = mlha
ff += 1
if ff >= 200:
break
moonrise = utcmoon - mlha
moonrise = timeadjust(moonrise, utcdis)
moonrise2 = decimaltominsecs(moonrise)
moonset = timeadjust(moonset, utcdis)
moonset2 = decimaltominsecs(moonset)
return ["{}:{}:{}".format(floor(moonrise),
moonrise2[0], moonrise2[1]),
"{}:{}:{}".format(floor(moonset),
moonset2[0], moonset2[1]),
ff, [moonrise, moonrise2, moonset, moonset2]]
In the risesetlist function moonArray is called as follows:
moonArray = self.calculate(y, m, d, h, mins, second)
and the within a while loop
moonArray = self.calculate(y, m, d, h, 0, 0)
So how then can variables be assigned from array locations [8],[6],[11] as in both array constructors only 0..5 exist?
msd = moonArray[8]
mra = moonArray[5]
mdecl = moonArray[6]
gmsto = moonArray[11]
So, I just need to know what the moonArray is doing in python so I can code up the java version. Any help would be appreciated.
I am making an application for teachers that you put in the max number of points and it tells you the percent of each amount wrong. I then would like to use that percentage to make each of the textviews display the grade with the corresponding percentage. So the first column is the amount right out of the max, the second column is the percentage, and the third column is the letter grade. I then have eight rows, but would like to expand that in the future. I know that I would have to an if to say if the percent is between two numbers, settext to a letter grade, but would I have to do the same for every textview, or what other way would there be to do this?
This is the code for what I basically have to far:
MaxInt = Integer.parseInt(number);
minus1 = MaxInt - 1;
minus2 = MaxInt - 2;
minus3 = MaxInt - 3;
minus4 = MaxInt - 4;
minus5 = MaxInt - 5;
minus6 = MaxInt - 6;
minus7 = MaxInt - 7;
minus8 = MaxInt - 8;
float PD1 = (minus1 * 100.0f) / MaxInt;
float PD2 = (minus2 * 100.0f) / MaxInt;
float PD3 = (minus3 * 100.0f) / MaxInt;
float PD4 = (minus4 * 100.0f) / MaxInt;
float PD5 = (minus5 * 100.0f) / MaxInt;
float PD6 = (minus6 * 100.0f) / MaxInt;
float PD7 = (minus7 * 100.0f) / MaxInt;
float PD8 = (minus8 * 100.0f) / MaxInt;
int D1 = (int) PD1;
int D2 = (int) PD2;
int D3 = (int) PD3;
int D4 = (int) PD4;
int D5 = (int) PD5;
int D6 = (int) PD6;
int D7 = (int) PD7;
int D8 = (int) PD8;
P1 = String.valueOf(D1);
P2 = String.valueOf(D2);
P3 = String.valueOf(D3);
P4 = String.valueOf(D4);
P5 = String.valueOf(D5);
P6 = String.valueOf(D6);
P7 = String.valueOf(D7);
P8 = String.valueOf(D8);
TV1.setText(Integer.toString(minus1));
TV2.setText(Integer.toString(minus2));
TV3.setText(Integer.toString(minus3));
TV4.setText(Integer.toString(minus4));
TV5.setText(Integer.toString(minus5));
TV6.setText(Integer.toString(minus6));
TV7.setText(Integer.toString(minus7));
TV8.setText(Integer.toString(minus8));
TV9.setText(P1 + "%");
TV10.setText(P2 + "%");
TV11.setText(P3 + "%");
TV12.setText(P4 + "%");
TV13.setText(P5 + "%");
TV14.setText(P6 + "%");
TV15.setText(P7 + "%");
TV16.setText(P8 + "%");
I know that some of that isn't nessesary like converting the float to a integer, then string, but I will fix that when I am done. TV17 - TV24 will be the letter grades.