Help me out!! Question is when I try to solve the assignment my answer is not perfect compared to the solution. Should I try again to close to get perfect answer without looking at the solution even if it takes a lot of time? or should I check solution code even though my result doesn't match. For example, using the bisection method to payoff outstanding debt within a year my answer is too overpaid. It takes a lot of time to get even my imperfect code. MY question is how do you guys study? I know it is not good check the solution first, but sometimes it takes a lot of time.
I am stuck in problem set 1-c using bisection thing.
Post your code here, or ona code paste site. If you do so here, putting a ``` above and below the code will turn on code quoting. For example: \(\text{```}\) x = 0 while (x < 10): print 'X is:', x count = x + 1 \(\text{```}\) becomes: ``` x = 0 while (x < 10): print 'X is:', x count = x + 1 ``` Then we can go over it any any issues you are having.
Here is my code below ------------ ----------------------------------------------- #Enter the input outstandingBalance = float(raw_input("Enter the outstanding balance on your credit card: ")) ann_interestRate = float(raw_input("Enter the annual credit card interest rate as a decimal: ")) #initiaialize variable monthly_IR = ann_interestRate / 12.0 balance = (outstandingBalance * (1 + monthly_IR)**12.0) numOfMonth = 0 monthly_low = outstandingBalance / 12.0 monthly_upp = (outstandingBalance * (1 + monthly_IR)**12.0) / 12.0 midpoint = (monthly_low + monthly_upp) / 2 while balance > 0: numOfMonth = 0 balance = (outstandingBalance * (1 + monthly_IR)**12.0) while balance > 0 and numOfMonth < 12: numOfMonth += 1 balance = balance - midpoint midpoint = (midpoint + monthly_upp) /2 midpoint = round(midpoint, 2) balance = round(balance, 2) print "RESULT" print "Monthly payment to pay off debt in 1 year: " , midpoint print "Number of months needed: " , numOfMonth print "Balance: " , balance
OK, so this is for bisection search. I see you only adjust things in one direction: balance = balance - midpoint midpoint = (midpoint + monthly_upp) /2 I can understand breaking out of the loop if you pay thing off... but I think you need to change your adjust ments for both directions. |dw:1440830761437:dw|
With this sort of goal, you need to be able to go back up if you go too low. |dw:1440830811380:dw| If you can only reduce something, you get wors rather than corrrecting up.
Now, in contrast, here is something that corrects up and down by half each time. Step 5 gets close enough for it to be considered good. |dw:1440830894985:dw|
This is the basic flaw in your method of looping. You have a way to make your payment get larger, but not really a way for it to get smaller. You need to evaluate if it has gotten close enough. So if you pay it off in 12 months with say a 1% margin for error, then you call it good. This is an epsilon-delta type idea. In calculus, there is this idea that by setting a close enough output, eplsilon, you can use it to find a close enough input, delta. So you can pick a value, like $1 or 10 cents... or you can use a percentage, like 1%. epsilon = outstandingBalance * .01 while abs(balance) > epsilon and numOfMonth < 12: do stuff to get closer test to see if you went too far etc.
If you start with payoing off half, obiously the interest would need to be huge for it to take more than 2 months. So it should reliably chop to 1/4th, then 1/8th... but then it is up for grabs. It will pribably go to 1/16th, but that will be too low, so it needs to adust up to half way beteween 1/8th and 1/16th... which is 3/32 an so on. Each step up or down it should jump half the distance back until what it does in 12 months is within epsilon of the answer.
thank you very much !! very helpful indeed
Join our real-time social learning platform and learn together with your friends!