I've been working on a Java project which is calculator which can be used for calculating different scenarios of compound interest (very similar to the TVM Function found on a graphics calculator like this one)
The main function of the calculator is to calculate missing values using the known values in a formula. I have gotten all of the formulas working except for the one which calculates Interest rate (I)
I have done some research and apparently there is no straight formula to calculate the interest rate. This website: http://www.getobjects.com/Components/Finance/TVM/formulas.html shows the method i need to use, but it requires some iteration to find I using trial and error. (Check the link, Scroll down to the heading "Interest Rate Per Year")
Here is the structure I have set up for it:
public static double calculateI(double N, double PV, double PMT, double FV, double PY){
//method for calculating I goes here
return I;
}
I am not sure how to implement this, could someone please suggest how this can be done or point me in the right direction?
Thanks in advance.
Here is my code after the suggestion made by @rocketboy
public static double formulaI(double ip, double N, double PV, double PMT, double FV, double PY){
double I1=(PV*Math.pow((1+ip),N))+((PMT*1)*(Math.pow((1+ip),N))-1)+FV;
return I1;
}
public static double calculateI(double N, double PV, double PMT, double FV, double PY){
double ip=0;
double res;
do{
res = formulaI(ip,N,PV,PMT,FV,PY);
ip=ip+0.01;
System.out.println(res);
}while(res!=0);
double I=ip*PY;
return I;
}