Bisection search [duplicate]

2024/11/18 18:37:18

Possible Duplicate:
Using bisection search to determine

I have posted other thread but it did not receive answers thus i'm trying to provide some of my work to make more clear.

I need to use bisection method to determine monthly payment in order to pay off debt in one year exactly.

Here's some code:

originalBalance = 320000
annualInterestRate = 0.2
monthly_interest = annualInterestRate / 12
low = originalBalance/12
high = (originalBalance*(1 + monthly_interest)**12)/12
epsilon = 0.01
min_payment = (high + low)/2.0while min_payment*12 - originalBalance >= epsilon:for month in range(0, 12):balance = (originalBalance - min_payment) * (1+monthly_interest)if balance < 0:low = min_paymentelif balance > 0:high = min_paymentmin_payment = (high + low)/2.0
print "lowest payment: " + str(balance)

However, I receive very way off answer: 298222.173851

My friend told me that correct answer is : 29157.09

Which is a lot lower than my...I guess the problem is in rounding(which I did not do yet) and preserving the balance after every looping and resetting it if balance is over 0. I cannot figure out how to attempt this problem and please, help someone :)

Answer

This is the correct one.

originalBalance = 320000
annualInterestRate = 0.2
monthly_interest = annualInterestRate / 12
low = originalBalance/12
high = (originalBalance*(1 + monthly_interest)**12)/12
epsilon = 0.01
min_payment = (high + low)/2.0while min_payment*12 - originalBalance >= epsilon:for month in range(0, 12):balance = (originalBalance - min_payment)/10 * (1+monthly_interest)if balance < 0:low = min_paymentelif balance > 0:high = min_paymentmin_payment = (high + low)/2.0
print "lowest payment: " + str(balance)
https://en.xdnf.cn/q/118641.html

Related Q&A

What am i doing wrong with matplotlibs yticks?

My code is as follows (dont ask for the variable names, im german^^):import matplotlib.pyplot as plt import numpy as np strecke = [] zeit = []daten = open("BewegungBeschleunigung.csv")for i i…

Python- How to implement quick sort when middle element is the pivot?

There are many different versions of quickSort that pick pivot in different ways.Always pick the first element or the last element as the pivot Pick a random element as a pivot. Pick median as the pivo…

How to expose a form when a radio button is checked?

Consider the following sample html template,<html><body><input type="radio" name="x">x</input><br><input type="radio" name="y"&g…

How to generate perlin noise in pygame?

I am trying to make a survival game and I have a problem with perlin noise. My program gives me this:But I want something like islands or rivers. Heres my code: #SetUp# import pygame, sys, random pygam…

Inputting range of ports with nmap optparser

This is the scriptimport nmap import optparsedef nmapScan(tgtHost,tgtPort):nmScan = nmap.PortScanner()nmScan.scan(tgtHost,tgtPort)state=nmScan[tgtHost][tcp][int(tgtPort)][state]print "[*] " +…

Implementing a Python algorithm for solving the n-queens problem efficiently

I am working on a project that requires me to solve the n-queens problem efficiently using Python. I have already implemented a basic recursive algorithm to generate all possible solutions, but I am lo…

Annotations with pointplot

I am using a pointplot in seaborn.import seaborn as sns sns.set_style("darkgrid") tips = sns.load_dataset("tips") ax = sns.pointplot(x="time", y="total_bill", hu…

How do I get data in tuples and tuples in lists?

I am trying to figure out the route that a car takes in a fictional manhattan. I have defined the starting position, being(1,2)(in a 2 dimensional grid).manhattan=[[1, 2, 3, 4, 5],[6, 7, 8, 9, 10],[11…

How to return a value from a table or dataframe given 2 inputs? Python

Lets say this is my dataframe:and the user inputs B and 2Then the function would return clemintineIs there a way to do this without using a bunch of if elif statements. The actual dataframe Im working …

tkinter progressbar for multiprocessing

I have a program that encrypts files and I used multiprocessing to make it faster, but I am having trouble with the tkinter progress bar. I have implemented it but it completes immediately or lags in b…