How to calculate the angle between two vectors? - java

We are trying to get the cos value between v and u but we are getting results much higher than 1 or lesser than 0
Where :
vx = in.nextInt(); // x speed of your pod
vy = in.nextInt(); // y speed of your pod
int ux = nextCheckPointIdX - x;
int uy = nextCheckPointIdY - y;
Here is the formula :
double cos = (vx*ux + vy*uy) / ( Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2)) + Math.sqrt(Math.pow(ux, 2) + Math.pow(uy, 2)) );
Do you find any errors in the previous line ?

The denominator was having the problem.
int num = (vx*ux + vy*uy);
double den = (Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2)) * (Math.sqrt(Math.pow(ux, 2) + Math.pow(uy, 2))) );
double cos = num / den;
System.out.println(cos);
System.out.println(Math.acos(cos));

Related

how can I use a counter controlled iteration in the print/math section on this square root program

I was thinking about adding a while loop to the program that'll loop for maybe 10 times instead of writing 10 println statements. but I found it hard since the value of x is different every time in the math section. I wrote this code before, now I want to shorten it. It's a square root finder program that uses the Babylonian method to find the square root of an integer between [S > 20 || S < 400]
int S;
System.out.print("Enter an integer, S: ");
S = myInput.nextInt();
if (S < 0) {
System.out.println("This program can not take the square root of a negative number.");
}
else if (S < 20 || S > 400) {
System.out.println("This value is out of range.");
}
else {
double a = S / 2.0;
double b = S / a;
double c = a + b;
double d = 0.5 * c;
// for x2
double e = S / d;
double f = d + e;
double g = 0.5 * f;
// for x3
double h = S / g;
double i = g + h;
double j = 0.5 * i;
// for x4
double k = S / j;
double l = j + k;
double m = 0.5 * l;
// for x5
double n = S / m;
double o = m + n;
double p = 0.5 * o;
// for x6
double q = S / p;
double r = p + q;
double s = 0.5 * r;
// for x7
double t = S / s;
double u = s + t;
double v = 0.5 * u;
// for x8
double w = S / v;
double x = v + w;
double y = 0.5 * x;
// for x9
double z = S / y;
double aa = y + z;
double ab = 0.5 * aa;
System.out.printf("%nx0 = %8.4f ", a);
System.out.printf("%nx1 = %8.4f ", d);
System.out.printf("%nx2 = %8.4f ", g);
System.out.printf("%nx3 = %8.4f ", j);
System.out.printf("%nx4 = %8.4f ", m);
System.out.printf("%nx5 = %8.4f ", p);
System.out.printf("%nx6 = %8.4f ", s);
System.out.printf("%nx7 = %8.4f ", v);
System.out.printf("%nx8 = %8.4f ", y);
System.out.printf("%nx9 = %8.4f ", ab);
}
All you need to do is set d to 2.0 outside the loop. Then use d in place of 2.0 inside the loop. The loop index of i is also used to name the iterations (x0, x1, x2, ...) when printing.
double d = 2.0; // set d to 2.0 here
for (int i = 0; i < 10; i++) {
double a = S / d; // use d here, the modified value will be used again
double b = S / a;
double c = a + b;
d = 0.5 * c;
System.out.printf("x%d = %8.4f%n", i, a);
}
prints the following for the input value of 50
x0 = 25.0000
x1 = 3.7037
x2 = 5.8127
x3 = 6.9374
x4 = 7.0698
x5 = 7.0711
x6 = 7.0711
x7 = 7.0711
x8 = 7.0711
x9 = 7.0711
Here is an exercise. Compare the current computed value to the last. If they are equal (or their difference is small), then you can exit the loop since repeated iterations won't improve the accuracy very much.

My code does exactly what it has to do, but i have no idea why

is supposed to calculate the coordinates of a projectile launched with respect to time (steps of 100ms), with a linear equation, and it outputs linear numbers, but if i plot this equation with CalcMe.com (math tool) it makes a parabolic plot
InVel = Double.parseDouble(jTextField1.getText());
g = Double.parseDouble(jTextField8.getText());
y = 1;
while(y >= -1) {
t += 100;
x = InVel * TimeUnit.MILLISECONDS.toSeconds(t) * Math.cos(45);
y = InVel * TimeUnit.MILLISECONDS.toSeconds(t) * Math.sin(45) - (1 / 2) * g * Math.pow(TimeUnit.MILLISECONDS.toSeconds(t), 2);
//System.out.print(Double.toString(x));
//System.out.printf(" ");
System.out.print(Double.toString(y));
System.out.printf("%n");
}
jTextField6.setText(Double.toString(x));
the code is in java
g is constant (9.8)
and invel is given by user so its constant too
g is the gravity and invel the initial velocity of the projectile
the equation is:x=invel*time*cos(45) and y=invel*time*sin(45)-(1/2)*g*t^2
anyone can help me?
Your milisecond to second value conversion method TimeUnit.MILLISECONDS.toSeconds(t) is the main fact. Its returning long value which one you are wanted double. Please take a look on below code. Probably its your answer. Just replace hard-coded value with your jTextField
public static void main(String[] args) {
double InVel = Double.parseDouble("10.555");
double g = Double.parseDouble("9.8");
double y = 1;
double x=0;
double t=0;
while(y >= -1) {
t += 100;
double timeInSeconds = (t / (double)1000) % (double)60;
x = InVel * timeInSeconds * Math.cos(45);
y = InVel * timeInSeconds * Math.sin(45) - ((double) 1 / (double) 2) * g * Math.pow(timeInSeconds, 2);
//System.out.print(Double.toString(x));
//System.out.printf(" ");
System.out.println("X = " + x + " Y = " + Double.toString(y));
System.out.printf("%n");
}
}

Solving a complex algebraic formula with java

I am trying to solve for y in the following equation using Java:
For the sake of visibility I divided the numerator and denominator into separate variables. I need to compute for x = -3 through x = 4 moving in increments of 0.5.
for(double x = -3; x <= 4; x += .5)
{
// Now we compute the formula for all values in between -3 and 4 in increments of 0.5
double top = ( 9 * Math.pow(x, 3) ) - ( 27 * Math.pow(x, 2) ) - ( (4 * x) + 12 );
double bottom = ( Math.pow(( 3 * Math.pow(x, 2) + 1 ) , 1/2) + Math.abs( 5 - (Math.pow(x, 4)) ) );
double y = top / bottom;
System.out.print("X = " + x + "\t Y = " + y);
}
The values I get are not as intended.
X = -3.0 Y = -6.311688311688312
X = -2.5 Y = -8.880570409982175
X = -2.0 Y = -15.333333333333334
X = -1.5 Y = -91.41176470588235
X = -1.0 Y = -8.8
X = -0.5 Y = -3.0105263157894737
X = 0.0 Y = -2.0
X = 0.5 Y = -3.305263157894737
X = 1.0 Y = -6.8
X = 1.5 Y = -45.529411764705884
X = 2.0 Y = -4.666666666666667
X = 2.5 Y = -1.429590017825312
X = 3.0 Y = -0.3116883116883117
X = 3.5 Y = 0.19940094137783482
X = 4.0 Y = 0.4603174603174603
Using an online tool I computed for X = 0 and got 2 instead of -2. Is there something wrong with how I did the math?
You made a mistake while implementing the expression.
... - ( (4 * x) + 12 )
Should be
... - (4 * x) + 12
Or in the complete expression:
double top = ( 9 * Math.pow(x, 3) ) - ( 27 * Math.pow(x, 2) ) - (4 * x) + 12;
Also as noted by #JacobG:
1/2 evaluates to 0, since it's an integer-division. This doesn't make any difference if you evaluate the for x = 0 though. This can be corrected using 0.5 instead.
There's a small typo in your equation:
1/2
Is equal to 0 in Java; see: Why is the result of 1/3 == 0?
To fix this, you can just type 0.5, or use 1 / 2D or 1D / 2.
See Paul's answer for another issue with your code.

Having trouble solving cubic equations in Java

I'm attempting to follow some psuedo code for solving cubic equations in Mathematics and Physics for Programmers, Chapter 3, as far as I can see I've followed it accurately, but I don't seem to be getting correct outputs.
For example: According to Wolfram Alpha 5x^3 + 4x^2 + 3x + 2 = 0 should give a root of x≈-0.72932, but I'm getting back -1.8580943294965526 from my script.
Can someone help me to work out what the hell I'm doing? I'm following the scripts to get a better understanding of maths and converting equations to code. But this is at the brink of my understanding so I'm finding it troublesome to debug. Coupled with the fact the book has no errata and many online reviews state the book has many errors, I'm struggling to see whether the issue is with my code, the books explanation or both.
The equation given in the book is:
Then if the discriminant > 0 then root has value of r+s:
if discriminant == 0 then there are two roots:
if discriminant < 0 you can find three roots as follows:
After finding t you can transform it to x by taking:
package com.oacc.maths;
public class SolveCubic {
public static double[] solveCubic(double a, double b, double c, double d) {
double[] result;
if (d != 1) {
a = a / d;
b = b / d;
c = c / d;
}
double p = b / 3 - a * a / 9;
double q = a * a * a / 27 - a * b / 6 + c / 2;
double D = p * p * p + q * q;
if (Double.compare(D, 0) >= 0) {
if (Double.compare(D, 0) == 0) {
double r = Math.cbrt(-q);
result = new double[2];
result[0] = 2 * r;
result[1] = -r;
} else {
double r = Math.cbrt(-q + Math.sqrt(D));
double s = Math.cbrt(-q - Math.sqrt(D));
result = new double[1];
result[0] = r + s;
}
} else {
double ang = Math.acos(-q / Math.sqrt(-p * p * p));
double r = 2 * Math.sqrt(-p);
result = new double[3];
for (int k = -1; k <= 1; k++) {
double theta = (ang - 2 * Math.PI * k) / 3;
result[k + 1] = r * Math.cos(theta);
}
}
for (int i = 0; i < result.length; i++) {
result[i] = result[i] - a / 3;
}
return result;
}
public static double[] solveCubic(double a, double b, double c) {
double d = 1;
return solveCubic(a, b, c, d);
}
public static void main(String args[]) {
double[] result = solveCubic(5, 4, 3, 2);
for (double aResult : result) {
System.out.println(aResult);
}
}
}
I also found this code example from the book site(n.b. this is not the psuedo code from the book): http://www.delmarlearning.com/companions/content/1435457331/files/index.asp?isbn=1435457331
on solveCubic(a,b,c,d)
--! ARGUMENTS:
a, b, c, d (all numbers). d is optional (default is 1)
--!
RETURNS: the list of solutions to dx^3+ax^2+bx+c=0
-- if d is defined then divide a, b and c by d
if not voidp(d)
then
if d=0 then return solveQuadratic(b,c,a)
set d to float(d)
set a to a/d
set b to b/d
set
c to c/d
else
set a to float(a)
set b to float(b)
set c to float(c)
end if
set p to b/3 - a*a/9
set q to a*a*a/27 - a*b/6 + c/2
set disc to p*p*p + q*q
if abs(disc)<0.001 then
set r to cuberoot(-q)
set ret to [2*r, -r]
else if disc>0 then
set r to cuberoot(-q+sqrt(disc))
set s to cuberoot(-q-sqrt(disc))
set ret to [r+s]
else
set ang to acos(-q/sqrt(-p*p*p))
set r to 2*sqrt(-p)
set ret to []
repeat with k=-1 to 1
set theta
to (ang - 2*pi*k)/3
ret.add(r*cos(theta))
end repeat
end if
set ret to ret-a/3 --NB: this adds the value to each
element
return ret
end
The error appears to be with the names of the parameters of your solveCubic method.
It seems your book is explaining how to solve the equation x3 + ax2 + bx + c = 0. You are calling your method thinking that the parameters a, b, c and d are for the equation ax3 + bx2 + cx + d = 0. However, it turns out that the body of your method is actually finding solutions to the equation dx3 + ax2 + bx + c = 0.
Aside from this naming error, the calculations appear to be correct. Try plugging your value ≈-1.858 into 2x3 + 5x2 + 4x + 3 if you don't believe me.
If you instead declare your solveCubic method as
public static double[] solveCubic(double d, double a, double b, double c) {
the parameters then correspond to the equation dx3 + ax2 + bx + c. You should then find that your method gives you the answer you expect.
Okay. So, first off the equations from the book seem to be referring to this idea: If you have an equation of the form:
Then by defining t as x - (a/3) you can transform this into an equation with no quadratic term, as verified by a bit of Mathematica:
Once you have no quadratic term, you can then apply the method given; let q be half the constant term, let p be one third the coefficient on the first power term, and define D (discriminant) as p*p*p - q*q.
All else then follows.
So why does your code not work? Because you've mislabeled the variables. a is the coefficient on x^2, not on x^3. If you call your method with the arguments (0.8, 0.6, 0.4, 1) or equivalently with (4, 3, 2, 5), you'll get the right answer.
(Or do as the other answer suggests and more around the variable names in the method declaration)
This works 100%. It has been tried and tested for all combinations.
public void solveCubicEquation(int A, int B, int C, int D) {
double a = (double) B / A;
double b = (double) C / A;
double c = (double) D / A;
System.out.println("Double values: ");
System.out.println(a + " " + b + " " + c);
double p = b - ((a * a) / 3.0);
double q = (2 * Math.pow(a, 3) / 27.0) - (a * b / 3.0) + c;
double delta = (Math.pow(q, 2) / 4) + (Math.pow(p, 3) / 27);
if (delta > 0.001) {
double mt1, mt2;
double t1 = (-q / 2.0) + Math.sqrt(delta);
double t2 = (-q / 2.0) - Math.sqrt(delta);
if (t1 < 0) {
mt1 = (-1) * (Math.pow(-t1, (double) 1 / 3));
} else {
mt1 = (Math.pow(t1, (double) 1 / 3));
}
if (t2 < 0) {
mt2 = (-1) * (Math.pow(-t2, (double) 1 / 3));
} else {
mt2 = (Math.pow(t2, (double) 1 / 3));
}
x1 = mt1 + mt2 - (a / 3.0);
} else if (delta < 0.001 && delta > -0.001) {
if (q < 0) {
x1 = 2 * Math.pow(-q / 2, (double) 1 / 3) - (a / 3);
x2 = -1 * Math.pow(-q / 2, (double) 1 / 3) - (a / 3);
} else {
x1 = -2 * Math.pow(q / 2, (double) 1 / 3) - (a / 3);
x2 = Math.pow(q / 2, (double) 1 / 3) - (a / 3);
}
} else {
System.out.println("here");
x1 = (2.0 / Math.sqrt(3)) * (Math.sqrt(-p) * Math.sin((1 / 3.0) * Math.asin(((3 * Math.sqrt(3) * q) / (2 * Math.pow(Math.pow(-p, (double) 1 / 2), 3)))))) - (a / 3.0);
x2 = (-2.0 / Math.sqrt(3)) * (Math.sqrt(-p) * Math.sin((1 / 3.0) * Math.asin(((3 * Math.sqrt(3) * q) / (2 * Math.pow(Math.pow(-p, (double) 1 / 2), 3)))) + (Math.PI / 3))) - (a / 3.0);
x3 = (2.0 / Math.sqrt(3)) * (Math.sqrt(-p) * Math.cos((1 / 3.0) * Math.asin(((3 * Math.sqrt(3) * q) / (2 * Math.pow(Math.pow(-p, (double) 1 / 2), 3)))) + (Math.PI / 6))) - (a / 3.0);
}
}
Note: will not work for imaginary values

Converting RGB to HSV

Hi I want convert from rgb to hsv and I have been following the algorithm from easyRGB.com. But doesn't work it show more red than normal. I rewrite the same algorithm a few time and revised, but I can't find the error. Any idea? There is the algorithm.
public static double[] RGB2HSV(double[] tmp){
double R = tmp[0] / 255.0;
double G = tmp[1] / 255.0;
double B = tmp[2] / 255.0;
double min = Math.min(Math.min(R, G), B);
double max = Math.max(Math.max(R, G), B);
double delta = max - min;
double H = max;
double S = max;
double V = max;
if(delta == 0){
H = 0;
S = 0;
}else{
S = delta / max;
double delR = ( ( ( max - R ) / 6 ) + ( delta / 2 ) ) / delta;
double delG = ( ( ( max - G ) / 6 ) + ( delta / 2 ) ) / delta;
double delB = ( ( ( max - B ) / 6 ) + ( delta / 2 ) ) / delta;
if(R == max){
H = delB - delG;
}else if(G == max){
H = (1/3) + delR - delB;
}else if(B == max){
H = (2/3) + delG - delR;
}
if(H < 0) H += 1;
if(H > 1) H -= 1;
}
double[] hsv = new double[3];
hsv[0] = H;
hsv[1] = S;
hsv[2] = V;
return hsv;
}
The values of 1/3 and (2/3) are 0, because you are operating with two integers, so the result is the integer too.
Use 1.0 / 3.0 and 2.0 / 3.0 instead.

Categories