Question about PS 1: problem 3, i wrote the code with the bisection method and it returns values close to test cases,but not the smallest monthly payment,coz the bisection method should get results which is just close to the answer! Here is my code: http://pastebin.com/r8bPAQAK
I don't think you understand the bisection method that well. Your code doesn't implement it. Study the bisection method again then try the problem. Take note that you programming is not just about writing codes. You should also understand the underlying algorithm well. :)
concur high and low should converge on a value - your code only adjusts low, there should be a conditional in there that also adjusts high. using print statements can help see what is going on http://pastebin.com/edRMErMz
i have modified it and it worked,but i wanna confirm it plz :), here is the new one : http://pastebin.com/ga8AuB1R i have read lecture 3 handout about bisection method,i hope i wrote it well,if there is anything wrong please enlighten me. Thanks alot :)
Problem 2 and 3 rely on similar concepts, so try to understand the solution to #2 before moving on to #3. I see a couple conceptual problems in your program: (1) Re: Line 9. The concept of the while loop is that you're going to keep the program re-iterating (trying new numbers, one at a time) when the balance is not paid off, ie: it is above zero. Continuing when below -1 and above 0 means you want it to stop when the balance is a negative fraction, which doesn't make sense. (2) Re: Line 13. The concept in the for loop is that you're trying out a number as the fixed payment by running it through 12 months, and if pays off the balance within that round of 12 months, end it at the month in which its paid off. If not, you're still in the while loop with a balance above zero, so start again at the original balance, and try another number (selecting it using bisection for problem 3 and increments of $10 for problem 2) to run through the 12 month loop. The problem is in Line 13 when you try to get back to the original balance. You have specified the start balance for month 1 with an if statement and no "else" for months 2-12, and the if-else setup doesn't do what you want. You want to run it 12 times the same way with each updated balance replacing the previous balance, not by specifying what to do in month 1 vs. months 2-12. For both issues, Line 9 and 13, you can see how it's done in the solution to Problem 2 and then do Problem 3 on your own.
looked good to me. The decision when to end the while loop depends on what the coder is trying to accomplish. For 'proof of concept', having an end balance between 0 and -1 is fine - its easy to code and it can always be refined later. The second post is workng - hi and lo converge on the answer like they should. If you uncomment the print statement and try it with different balances and interest rates you'll see that the number of iterations to find the answer doesn't change much compared to the input - pretty cool feature of bisection algorithms. http://dpaste.com/762694/
Thanks alot guys :) and a special -thank you- to bwCA ,you are a really helpful one :)
Join our real-time social learning platform and learn together with your friends!