I have the following code which gives the price given a certain rate:
public static void main(String[] args) {
double term = 5;
double coupon = 5;
double rate = 0.0432;
//should get the price == 103;
double price;
double sum = 0;
for (int i = 1; i <= term; i++) {
sum = sum + (coupon / Math.pow((1 + rate), i));
}
double lastAmount = (100 / Math.pow((1 + rate), (term)));
price = sum + lastAmount;
System.out.println("price is:" + price);
}
My question is, how can I do this other way around? where the price is given and the rate is unknown. So I figured out, it's going to be an exponential equation. where it should look like below:
103x^5 - 5x^4 - 5x^3 - 5x^2 - 5x - 105=0
where x = 1+rate
solving the exponential equation, you get x = 1.0432 which means the rate is 0.0432 as expected.
How can I implement this in java to solve the equation.