I'm trying to take user input in the form of myMonthlyPayment, myAnnualInterestRate, and myPrincipal in order to calculate the number of months needed to pay off debt by using The formula I've attached to this post. What I have in eclipse for the formula right now is:
monthsNeeded = ((Math.log(myMonthlyPayment) - Math.log(myMonthlyPayment)
- ((myAnnualInterestRate / 1200.0) * myPrincipal))
/ ((Math.log(myAnnualInterestRate) / 1200.0) + 1.0));
I should be getting an output of 79 months with the inputs I'm using but instead I'm getting -62. I know the formula is correct, I'm almost positive I've made a mistake somewhere in the translation of it into Java. If someone could point it out that would be greatly appreciated!
So I've fixed it, with a sample input and output.
I didn't put much effort into making this code beautiful but you can see that even separating it into 3 parts using method extraction (although I didn't know how to name them, lacking the domain knowledge) made the code easier to understand.
public class Example {
public static void main(String[] args) {
double myMonthlyPayment = 2000;
double myAnnualInterestRate = 5;
double myPrincipal = 200000;
System.out.println(a(myMonthlyPayment));
System.out.println(b(myPrincipal, myAnnualInterestRate, myMonthlyPayment));
System.out.println(c(myAnnualInterestRate));
double monthsNeeded = (a(myMonthlyPayment) - b(myPrincipal, myAnnualInterestRate, myMonthlyPayment))
/ c(myAnnualInterestRate);
System.out.println(monthsNeeded);
}
private static double c(double myAnnualInterestRate) {
return Math.log((myAnnualInterestRate / 1200.0) + 1);
}
private static double b(double myPrinicipal, double myAnnualInterestRate, double myMonthlyPayment) {
return Math.log(myMonthlyPayment - (myAnnualInterestRate / 1200.0) * myPrinicipal);
}
private static double a(double myMonthlyPayment) {
return Math.log(myMonthlyPayment);
}
}
I think this is what you're looking for:
monthsNeeded = (Math.log(myMonthlyPayment) - Math.log(myMonthlyPayment - myAnnualInterestRate / 1200d * myPrincipal)) / Math.log(myAnnualInterestRate / 1200d + 1);
It seems that, in your solution, you weren't calculating your myAnnualInterestRate/1200*myPrincipal inside your second Math.log(...). You had also left some calculations outside of Math.log(...) in the bottom half of your equation.
If you have an equation that does an operation inside a natural log, when you convert that equation to Java code, the operation needs to still be done, inside the natural log:
ln(someNumber + 10)
would be converted to:
Math.log(someNumber + 10),
NOT:
Math.log(someNumber) + 10
Hope this helps and good luck. :)
Related
I need to find the percentage change of different numbers, I've got the formula down but the result keep ending up being minus instead of a positive e.g. 100 to 150 comes out a -50.00% instead of 50.00%. Thanks in advance!
package package2;
import java.util.Scanner;
class Selection3 {
public static void main (String [] args)
{
perChange();
}
public static void perChange() {
double perCha0, perCha1, perCha2, perCha3, perCha4, perCha5;
perCha0 = ((38 - 108)*100/38);
perCha1 = ((35 - 63)*100/35);
perCha2 = ((4 - 6)*100/4);
perCha3 = ((3 - 5)*100/3);
perCha4 = ((20 - 40)*100/20);
perCha5 = ((100 - 150)*100/100);
System.out.println(perCha0);
System.out.println(perCha1);
System.out.println(perCha2);
System.out.println(perCha3);
System.out.println(perCha4);
System.out.println(perCha5);
}
output
-184.0
-80.0
-50.0
-66.0
-100.0
-50.0
The problem you have is a math problem, and you have encoded a bad formula into your programming.
A percentage of change is a "difference of change" divided by the original amount. When 30 items become 60 items, it is a difference of +30; but, because you subtract the numbers in the wrong order, you get an incorrect "difference" of -30.
Subtracting is not like addition, changing the order of the numbers doesn't result in the same result.
You have a math problem. For example in you first formula:
38 * 100 / 108 = 35.15%
You have basically 3 options:
Use Math.abs()
perCha0 = (Math.abs(38 - 108)*100/38);
// or
perCha0 = (Math.abs(x1 - x2)*100/x1);
If else condition
if(x2 > x1)
perCha0 = ((x2 - x1)*100/x1);
else
perCha0 = ((x1 - x2)*100/x1);
If you know before that you are calculating for percentage increase i.e. x2 > x1
perCha0 = ((x2 - x1)*100/x1);
You can also simplify the above if you want.
I have a program that takes in anywhere from 20,000 to 500,000 velocity vectors and must output these vectors multiplied by some scalar. The program allows the user to set a variable accuracy, which is basically just how many decimal places to truncate to in the calculations. The program is quite slow at the moment, and I discovered that it's not because of multiplying a lot of numbers, it's because of the method I'm using to truncate floating point values.
I've already looked at several solutions on here for truncating decimals, like this one, and they mostly recommend DecimalFormat. This works great for formatting decimals once or twice to print nice user output, but is far too slow for hundreds of thousands of truncations that need to happen in a few seconds.
What is the most efficient way to truncate a floating-point value to n number of places, keeping execution time at utmost priority? I do not care whatsoever about resource usage, convention, or use of external libraries. Just whatever gets the job done the fastest.
EDIT: Sorry, I guess I should have been more clear. Here's a very simplified version of what I'm trying to illustrate:
import java.util.*;
import java.lang.*;
import java.text.DecimalFormat;
import java.math.RoundingMode;
public class MyClass {
static class Vector{
float x, y, z;
#Override
public String toString(){
return "[" + x + ", " + y + ", " + z + "]";
}
}
public static ArrayList<Vector> generateRandomVecs(){
ArrayList<Vector> vecs = new ArrayList<>();
Random rand = new Random();
for(int i = 0; i < 500000; i++){
Vector v = new Vector();
v.x = rand.nextFloat() * 10;
v.y = rand.nextFloat() * 10;
v.z = rand.nextFloat() * 10;
vecs.add(v);
}
return vecs;
}
public static void main(String args[]) {
int precision = 2;
float scalarToMultiplyBy = 4.0f;
ArrayList<Vector> velocities = generateRandomVecs();
System.out.println("First 10 raw vectors:");
for(int i = 0; i < 10; i++){
System.out.print(velocities.get(i) + " ");
}
/*
This is the code that I am concerned about
*/
DecimalFormat df = new DecimalFormat("##.##");
df.setRoundingMode(RoundingMode.DOWN);
long start = System.currentTimeMillis();
for(Vector v : velocities){
/* Highly inefficient way of truncating*/
v.x = Float.parseFloat(df.format(v.x * scalarToMultiplyBy));
v.y = Float.parseFloat(df.format(v.y * scalarToMultiplyBy));
v.z = Float.parseFloat(df.format(v.z * scalarToMultiplyBy));
}
long finish = System.currentTimeMillis();
long timeElapsed = finish - start;
System.out.println();
System.out.println("Runtime: " + timeElapsed + " ms");
System.out.println("First 10 multiplied and truncated vectors:");
for(int i = 0; i < 10; i++){
System.out.print(velocities.get(i) + " ");
}
}
}
The reason it is very important to do this is because a different part of the program will store trigonometric values in a lookup table. The lookup table will be generated to n places beforehand, so any velocity vector that has a float value to 7 places (i.e. 5.2387471) must be truncated to n places before lookup. Truncation is needed instead of rounding because in the context of this program, it is OK if a vector is slightly less than its true value, but not greater.
Lookup table for 2 decimal places:
...
8.03 -> -0.17511085919
8.04 -> -0.18494742685
8.05 -> -0.19476549993
8.06 -> -0.20456409661
8.07 -> -0.21434223706
...
Say I wanted to look up the cosines of each element in the vector {8.040844, 8.05813164, 8.065688} in the table above. Obviously, I can't look up these values directly, but I can look up {8.04, 8.05, 8.06} in the table.
What I need is a very fast method to go from {8.040844, 8.05813164, 8.065688} to {8.04, 8.05, 8.06}
The fastest way, which will introduce rounding error, is going to be to multiply by 10^n, call Math.rint, and to divide by 10^n.
That's...not really all that helpful, though, considering the introduced error, and -- more importantly -- that it doesn't actually buy anything. Why drop decimal points if it doesn't improve efficiency or anything? If it's about making the values shorter for display or the like, truncate then, but until then, your program will run as fast as possible if you just use full float precision.
In the code below I wanted to get sample standard deviation but I got (population standard deviation) instead of (sample standard
deviation), What am I doing wrong?
public void compute(View view) {
no1 = Double.parseDouble(et1.getText().toString());
no2 = Double.parseDouble(et2.getText().toString());
no3 = Double.parseDouble(et3.getText().toString());
m = (no1 + no2 + no3)/3;
mm1= (no1-m);
mm1 = mm1*mm1;
mm2= (no2-m);
mm2 = mm2*mm2;
mm3= (no3-m);
mm3 = mm3*mm3;
std = (mm1+mm2+mm3)/3;
tv1.setText(String.valueOf(Math.sqrt(std)));
}
If you're trying to calculate an estimate of the population using a random sample of that population (the "sample standard deviation") then the calculation is almost the same, but the dividend needs to be decreased by one.
In other words, your sample size is three so you need to divide by two in order to adjust for the fact you're working from a sample and not the entire population. So your final line of calculation needs to look like this:
std = (mm1 + mm2 + mm3) / 2;
You can find numerous pages online which give a detailed explanation about the difference between population and sample standard deviation, such as this article on macroption.com.
I tried to make a program (in Java) that calculates pi with the Chudnovsky algorithm but it has the output NaN (Not a Number). Please help me find mistakes in my code, or improve my code. (I don't have a lot of Java programming knowledge)
You can find Chudnovsky's algorithm here:
https://en.wikipedia.org/wiki/Chudnovsky_algorithm
here is my code:
package main;
public class Class1 {
public static void main(String[] args)
{
double nr1=0,nr2=0,nr3=0,pi=0;
int fo1=1, fo2=1, fo3=1;
for(int i=0; i<=20; i++){
for(int fl1=1; fl1<=(6*i); fl1++){fo1 = fo1 * fl1;}
for(int fl2=1; fl2<=(3*i); fl2++){fo2 = fo2 * fl2;}
for(int fl3=1; fl3<=(i); fl3++){fo3 = fo3 * fl3;}
nr1 = ( (Math.pow(-1, i)) * (fo1) * ((545140134*i) + 13591409) );
nr2 = ( (fo2) * (Math.pow(fo3, i)) * ( Math.pow(Math.pow(640320, 3), (i+(1/2)) )) );
nr3 = 12 * (nr1/nr2);
}
pi = 1/nr3;
System.out.println((Math.PI));
System.out.println(pi);
}
}
There are many issues here.
As Andy mentioned, 1/2 is not 0.5.
You are using integers to compute things like 120! which is completely out of bounds for any primitive type.
f01,f02,f03 should be initialized inside each loop, otherwise they grow even bigger
It is not trivial to fix it. You can take a look at
Error calculating pi using the Chudnovsky algorithm - Java
and
http://www.craig-wood.com/nick/articles/pi-chudnovsky/
for some hints, but don't expect built-in primitive types to work with that algorithm.
I need your help, and thank you for reading my question!
I am currently writing a java Programm that will use an Direket Form 2 Transposed Filter. I know that the function filter in Matlab will do that just fine, but i have to use Java.
So does anyone know you to implement this Direkt Form 2 Transposed , this Math Function:
y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)
- a(2)*y(n-1) - ... - a(na+1)*y(n-na)
in any Programmm Language? All it takes is hopefully a point to the wrigth direction so i can figure it out! Maybe there is an C Lib that implements some of the matlab functions, just anything.
So thank you for your time
yours Elektro
Follow up:
I tried for a couple of days to understand your function but i couldn't.
This is the function from Matlab: filter
http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/filter.html&http://www.google.de/search?hl=de&q=filter+matlab&btnG=Google-Suche&meta=&aq=f&oq=
All i know is that i use in matlab the function like this:
newArray = filter(1,LPC_Faktor,OldArray)
All I have to do is to implement the filter function.
So could you help again?
Thanks
Elektro
Whatever language you use, the direct form II transposed structure is quite simple.
For example, in C, it could be something like:
float myFilter( float u)
{
static float[nb] x = {0,0,0,...,0); // initialize x
static float[na] y = {0,0,0,...,0); // initialize y
static float b1 = ....; // put b(1) here
static float[nb] b = {...,...,...,...,...}; // put b(2) to b(nb+1) here
static float[na] a = {...,...,...,...,...}; // put a(2) to a(na+1) values here
// initialization
float sum = 0;
int i=0;
// compute the value
for(i=0;i<nb;i++)
sum += b[i]*x[i];
for(i=0;i<na;i++)
sum -= a[i]*y[i];
sum += b1*u;
// prepare the values for the next time
for(i=1;i<nb;i++)
x[i] = x[i-1];
x[0] = u;
for(i=1;i<na;i++)
y[i] = y[i-1];
y[0] = sum;
// return the value
return sum;
}
I did not test the code, but it is something like that.
The Direct Form II transposed is the simplest form to implement a FIR filter (numerically, and specially in fixed-point, it is not the best, but it is the form that requires the less operations).
Of course, it is possible to have a better implementation (with cycling array, for example). If needed, I can provide it, too.
EDIT: I answered too quickly. The algorithm you provide
y(n) = b(1)x(n) + b(2)x(n-1) + ... + b(nb+1)x(n-nb) - a(2)y(n-1) - ... - a(na+1)*y(n-na)
is not the Direct Form II, but the direct form I. It requires to store na+nb values (n is the order of your filter), whereas the Direct Form II requires only max(na,nb).
The algorithm used for the Direct Form II is
e(n) = u(n) - a(1)*e(n-1) - a(2)*e(n-2) - ... - a(na)*e(n-na)
y(n) = b(1)*e(n-1) + b(2)*e(n-2) + ... + b(nb)*e(n-nb)
Tell me if you need this form or not.
after long searching i found the answer,
thank you showed the rigth way:
filter(int ord, float *a, float *b, int np, float *x, float *y)
{
int i,j;
y[0]=b[0] * x[0];
for (i=1;i<ord+1;i++)
{
y[i]=0.0;
for (j=0;j<i+1;j++)
y[i]=y[i]+b[j]*x[i-j];
for (j=0;j<i;j++)
y[i]=y[i]-a[j+1]*y[i-j-1];
}
/* end of initial part */
for (i=ord+1;i<np+1;i++)
{
y[i]=0.0;
for (j=0;j<ord+1;j++)
y[i]=y[i]+b[j]*x[i-j];
for (j=0;j<ord;j++)
y[i]=y[i]-a[j+1]*y[i-j-1];
}
} /* end of filter */