source('./utils.R') ## Part (a): ## P = 100000 n = 30 r = 0.08 A = annuity_payment(P, n, r) print(A) ## Part (b): ## current_debt = c(P) n= 5 for( yi in seq(1, n) ){ lcdi = length(current_debt) debt_plus_interest = current_debt[lcdi] * (1+r) current_debt = c(current_debt, debt_plus_interest-A) } print('current_debt=') print(current_debt) ## Part (c): ## lcdi = length(current_debt) P_now = current_debt[lcdi] # the amount still to be paid r = 0.09 n = 25 A_prime = annuity_payment(P_now, n, r) print(A_prime) ## Part (d): ## root_fn = function(n, P, A, r){ err = P - annuity_PV(A, n, r) return(err) } ## Lets get an idea about how many more years are needed untill we pay off the loan: ## ns = seq(35, 40, length.out=20) plot(ns, root_fn(ns, P_now, A, r), type='l', xlab='n') abline(h=0, col='blue') grid() source('http://waxworksmath.com/Authors/A_F/Conte/Code/Chapter3/utils.R') ##source('../../../../Conte/Code/Chapter3/utils.R') rt = bisection_method(35, 40, function(x) {root_fn(x, P_now, A, r)}) print(rt) n = rt[1] # number of years needed to payoff the new loan print(sprintf('years_till_payed= %.3f', n))