6.00: Introduction to Computer Science and Programming
Problem Set 4: Successive Approximation
Handed out: Tuesday, October 6, 2009
Due date:
11:59pm Tuesday, Oct 13,
2009
Successive approximation is a problem-solving method where you try to guess the right answer to a problem and then check your guess. If the guess is good enough, you're done. Otherwise, you keep improving and checking your guess in small increments, getting closer and closer to the right answer, until you determine that the guess is good enough.
In this problem set, we will look at Newton's method, which uses successive approximation to find the roots of a function, and the golden ratio, which is a numerical value that can be found using successive approximation.
Workload
Please let us know how long you spend on each problem. We want to be
careful not to overload you by giving out problems that take longer
than we anticipated.
Collaboration
Please take note
of the policy in
the
course information handout.
Newton's method (also known as the Newton-Raphson method) is a successive approximation method for finding the roots of a function. Recall that the roots of a function f(x) are the values of x such that f(x) = 0. You can read more about Newton's method here.
Here is how Newton's method works:
For simplicity, we will only be using polynomial functions in this problem set.
For this problem set, we will be representing polynomials as tuples. The index of the tuple represents the power, and the value at that index represents the coefficient for that term. So for example, the polynomial x4 + 3x3 + 17.5x2 - 13.39 would be represented by the tuple (-13.39, 0, 17.5, 3, 1). This is because the tuple represents -13.39x0 + 0x1 + 17.5x2 + 3x3 + 1x4, which is the same as x4 + 3x3 + 17.5x2 - 13.39.
You may find the Python power operator ** useful.
As stated before, we will need to find f '(xn). f '(x) is what's known as the derivative of f(x). For those of you who have not taken calculus, the following is all you will need to know about derivatives to do this problem set.
Since we're only dealing with polynomial functions, we only need to know how to calculate the derivative of a polynomial function. Consider a simple polynomial function f(x) = axb. The derivative of f(x) is f '(x) = (a*b)xb-1. In other words, you multiply the coefficient by the power, and then decrement the power by one.
To compute the derivative of a polynomial function with many terms, you just do the same thing to every term individually. For example, consider f(x) = x4 + 3x3 + 17.5x2 - 13.39.
The derivative of x4 is 4x3.
The derivative of 3x3 is 9x2.
The derivative of 17.5x2 is 35x.
The derivative of -13.39 is 0.
We add all those terms up and get f '(x) = 4x3 + 9x2 + 35x.
The function starts at x_0. It then applies Newton's method. It ends when it finds a root x such that the absolute value of f(x) is less than epsilon, i.e. f(x) is close enough to zero. It returns the root it found as a float.
The golden ratio is a special number in mathematics, represented by the Greek letter phi (φ). What's special about it is that the ratio between 1 and φ is the same as the ratio between φ and 1 + φ. In other words,
φ = (φ + 1)/φ
You can read more about the golden ratio here. While there are straightforward analytical approaches to calculating the value of φ, we'll be taking a look at how to compute φ by successive approximation.
This problem is primarily meant to get you to think about how you can use previously-defined functions to solve new problems. Your code for this problem should be very short, fewer than four lines of code.
This completes the problem set!
2. Time and collaboration info
At the start of the file, in a comment, write down the number of hours (roughly) you spent on this problem set, and the names of whomever you collaborated with. For example:
# Problem Set 4 # Name: Jane Lee # Collaborators (Discussion): John Doe # Collaborators (Idential Solution): Jane Smith # Time: 1:30 # .... your code goes here ...
3. Submit
Upload the
file to your
workspace. If there is some error uploading to your workspace,
email the file to 6.00-staff [at] mit.edu.
You may upload (or email) new versions of the problem set until the 11:59pm deadline, but anything uploaded after that will be ignored.