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.
Related
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");
}
}
Im trying to find the components/head of a 3D vector with in java. I have a already got the x,y,z variables setup and the corresponding rotation variables. Using these variables and vector math, I want to find the components after they have been rotated. You can think the x,y,z variables as a vector being translated.
Using the math from this post:
Rotating a Vector in 3D Space
I wrote some code that was supposed to calculate position based on that last post:
//Rotate Z
x = (( x * Math.cos(radz)) - (y * Math.sin(radz)));
y = (( x * Math.sin(radz)) + (y * Math.cos(radz)));
//Ignore Z ###############################################
//Rotate Y
x = (( x * Math.cos(rady)) - (z * Math.sin(rady)));
//Ignore Y ###############################################
z = (( x * Math.sin(rady)) + (z * Math.cos(rady)));
//Rotate X
//Ignore X ###############################################
y = (( y * Math.cos(radx)) - (z * Math.sin(radx)));
z = (( y * Math.sin(radx)) + (z * Math.cos(radx)));
Where x, y and z are the positions that need to be changed and radx, rady and radz are the degrees of rotation in radians.
Using this code, if you set the variables like so:
double radx = Math.toRadians(0f);
double rady = Math.toRadians(90f);
double radz = Math.toRadians(0f);
double x = 1;
double y = 0;
double z = 0;
System.out.println(x + " " + y + " " + z);
It outputs:
6.123233995736766E-17 0.0 6.123233995736766E-17
Which im fairly sure isn't accurate. . .
What am I doing wrong with this code? Is there a easier way to find the head of a 3D vector java?
Also I do have the joml library, but it seems to have the same issue with the vec.rotateX method.
You are updating your variables early. Try to:
//Rotate Z
double newX = (( x * Math.cos(radz)) - (y * Math.sin(radz)));
y = (( x * Math.sin(radz)) + (y * Math.cos(radz)));
//Ignore Z ###############################################
x = newX;
//Rotate Y
newX = (( x * Math.cos(rady)) + (z * Math.sin(rady)));
//Ignore Y ###############################################
z = (( x * -Math.sin(rady)) + (z * Math.cos(rady)));
x = newX;
//Rotate X
//Ignore X ###############################################
double newY = (( y * Math.cos(radx)) - (z * Math.sin(radx)));
z = (( y * Math.sin(radx)) + (z * Math.cos(radx)));
y = newY;
I need to convert the following python code to Java and remember from the past that handling decimal values must be done carefully otherwise the results will not be accurate.
My question is, can I use doubles for all non-interger values given doubles are more accurate that floats?
Here is my start of the conversion:
Python code
def degrees(rads):
return (rads/pi)*180
def radians(degrees):
return (degrees/180 * pi)
def fnday(y, m, d, h):
a = 367 * y - 7 * (y + (m + 9) // 12) // 4 + 275 * m // 9
a += d - 730530 + h / 24
return a
Java conversion
public double degress(double rads)
{
return (rads/PI)*180;
}
public double radians(double degrees)
{
return (degrees/180 * PI);
}
public double fnday(int y, int m, int d, int h)
{
double a = 367 * y - 7 * (y + (m + 9) / 12) / 4 + 275 * m / 9;
a += d - 730530 + h / 24;
return a;
}
I know it may be a simple answer but I need to know the postion of the moon and sun for the app and do not want to rely on an api for this data. I simple want to put in the latitude and longitdue and get the sun and moon rise and set times.
Using a double for each variable would suffice; however, you have an issue that results from integer division:
double a = 367 * y - 7 * (y + (m + 9) / 12) / 4 + 275 * m / 9;
If I were you, I'd change y, m, d, and h to all be doubles, so you retain the decimal places when dividing:
public double fnday(double y, double m, double d, double h) {
double a = 367 * y - 7 * (y + (m + 9) / 12) / 4 + 275 * m / 9;
a += d - 730530 + h / 24;
return a;
}
If you need a really big precision, the best way is use,
java.lang.BigDecimal, that extends from java.math.Number.
You can even use your existing doubles if you need:
double d = 67.67;
BigDecimal bd = new BigDecimal(d);
But you will need to use the methods from the class like this:
public BigDecimal degress(BigDecimal rads)
{
BigDecimal pi = new BigDecimal(PI);
return (rads.divide(pi))*180;
}
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));
Let's say I have a simple line chart with 5 values (a = 155, b = 200, c = 250, d = 300, e 0 345)
I need a way to calculate which values go on the Y-axis, in such a way that the values look nice. I also want to see the minor steps.
If I use a simple formula I would do this:
MaxValue - Minvalue = difference
300- 900 = 600
For 5 steps: 600/5 = 120 per step
That would lead to these values for the Y-axis:
Y0 = 200.0 (Rounding off to 200)
Y1 = 360.0 (Rounding off to 400)
Y2 = 520.0 (Rounding off to 600)
Y3 = 680.0 (Rounding off to 700)
Y4 = 840.0 (Rounding off to 900)
Y4 = 1000.0 (Rounding off to 1000)
What I actually would like is the values to be:
Y0 = 200
Y1 = 400
Y2 = 600
Y3 = 800
Y4 = 1000
But how do I calculate this?
Before calculation I don't know the magnitude of the values, it could be also like thousands, or tens.
Not exactly what you expect, but may give you an idea:
const int N = 6;
double vals[N] = {200.0, 360.0, 520.0, 680.0, 840.0, 1000.0};
for (int i = 0; i < N; i++) {
double factor = pow(10.0, floor(log10(vals[i])));
double v = floor(vals[i] / factor + 0.5) * factor;
std::cout << vals[i] << " " << v << std::endl;
}
P.S. Sorry, it's in C++ but you can easily translate it to Java.