Fórum Root.cz
Hlavní témata => Vývoj => Téma založeno: Radim 11. 10. 2012, 14:27:42
-
Ahoj,
potrebuji pomoc s Pythonem. Mam za ukol spocitat mesicni splatky kreditni karty, tak ze cela dluzna castka musi byt zaplacena behem 12 mesicu a to vcetne uroku. S kazdou mesicni splatkou se snizuji uroky, ktere je potreba doplatit.
Musim pouzit metodu bisekce.
Dluzna castka je = 320000
rocni urok je = 0.2
spravny vysledek je = 29157.09 (coz je mesicni splatka)
Muzete mi nekdo pomoci s kodem? Zatim mam tohle ale nefunguje to spravne.
balance = 320000
annualInterestRate = 0.2
originalBalance = balance
monthly_interest = annualInterestRate / 12
x = balance
epsilon = 0.01
numGuesses = 0
low = originalBalance/12
high = (originalBalance*(1 + monthly_interest)**12)/12
ans = (high + low)/2.0
while abs(ans**2 - x) >= epsilon:
print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))
numGuesses += 1
for month in range(0, 12):
balance = (originalBalance - ans) * (1+monthly_interest)
if ans**2 < 0:
low = ans
else:
high = ans
ans = (high + low)/2.0
print('numGuesses = ' + str(numGuesses))
print(str(ans) + ' is close to square root of ' + str(x))
-
ahoj, co mas v kodu konkretne zle, nevim, ale zda se me podezrela tato konstrukce:
if ans**2 < 0:
low = ans
Nevim, ale ucili me, ze cokoli na druhou bude vzdy kladne cislo, taze se to do teto vetve nikdy nedostane.
-
http://stackoverflow.com/questions/12804670/using-bisection-search-to-determine
-
Už to mám
balance = 999999
annualInterestRate = 0.18
originalBalance = balance
monthly_interest = annualInterestRate / 12
x = balance
epsilon = 0.01
low = originalBalance/12
high = (1 + annualInterestRate) * low
ans = (high + low)/2.0
while abs(x) > epsilon:
x = balance
for month in range(0, 12):
x = (x - ans) * (1+monthly_interest)
if x > 0:
low = ans
else:
high = ans
ans = (high + low)/2.0
print 'Lowest Payment:', round(ans, 2)