P = 100000.00 rate = 0.1 term = 30 ## Under monthly payments: ## n = 12*term r = rate/12 x = ( r * (1+r)^n * P )/( (1+r)^n - 1 ) total_payed = n*x interest_payed_montly = total_payed - P print(sprintf('Monthy: x= %.2f; total_payed= %.2f; interest_payed= %.2f', x, total_payed, interest_payed_montly) ) ## Under biwekly payments: ## r = rate/26 A = x/2 # what we pay every two weeks root_fn = function(y){ n = 26*y err = P - (A/r) * (1 - 1/(1+r)^n ) return(err) } ## Lets get an idea about how many years till be pay off the loan: ## y = seq(18, 24, length.out=20) plot(y, root_fn(y), type='l') abline(h=0, col='blue') grid() ## Find the value of y the loan is payed off: ## source('http://waxworksmath.com/Authors/A_F/Conte/Code/Chapter3/utils.R') ##source('../../../../Conte/Code/Chapter3/utils.R") rt = bisection_method(18, 24, root_fn) print(rt) y = rt[1] # number of years n = round(26*y) # number of biweekly payments total_payed = n*x/2 interest_payed_biweekly = total_payed - P print(sprintf('Biweekly: years_till_payed= %.3f; n_payments= %.1f', y, n)) print(sprintf('Biweekly: x/2= %.2f; total_payed= %.2f; interest_payed= %.2f', x/2, total_payed, interest_payed_biweekly)) savings_in_interest = interest_payed_montly - interest_payed_biweekly print(sprintf('Interest Savings of Biweekly over Monthly= %.2f', savings_in_interest))