-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed rate bisection search.py
More file actions
37 lines (31 loc) · 969 Bytes
/
Copy pathfixed rate bisection search.py
File metadata and controls
37 lines (31 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 19 12:05:07 2017
@author: thelma
"""
"""
Calculate lowest monthly rate for credit to be paid off in 12 months '/'
using bisection search.
Input: positive int balance, positive float annualInterestRate
Output: lowest monthly rate as float, rounded to 2 decimals.
"""
balance = 320000 # Value given
annualInterestRate = 0.2 # Value given
low = balance / 12
high = balance * ((1+annualInterestRate/12)**12) / 12
while True:
testbalance = balance
guess = (high+low) / 2
# Calculate total annual credit including interest
for m in range(12):
unpaid = testbalance - guess
testbalance = unpaid + unpaid * annualInterestRate / 12
# Bisection search
if round(testbalance, 2) < 0:
high = guess
elif round(testbalance, 2) > 0:
low = guess
else:
print('Lowest Payment: ' + str(round(guess, 2)))
break