Extra Homework 1
Due by 11:59pm on Tuesday, 9/24/2018
Instructions
Download extra01.zip. Inside the archive, you will find a file called extra01.py, along with a copy of the Ok autograder.
Submission: When you are done, submit with
python3 ok --submit
. You may submit more than once before
the deadline; only the final submission will be scored.
Using Ok
The ok
program helps you test your code and track your progress.
The first time you run the autograder, you will be asked to log in with your
@berkeley.edu account using your web browser. Please do so. Each time you run
ok, it will back up your work and progress on our servers.
You can run all the doctests with the following command:
python3 ok
To test a specific question, use the -q
option with the
name of the function:
python3 ok -q <function>
By default, only tests that fail will appear. If you
want to see how you did on all tests, you can use the -v
option:
python3 ok -v
If you do not want to send your progress to our server or you have any
problems logging in, add the --local
flag to block all
communication:
python3 ok --local
When you are ready to submit, run ok
with the
--submit
option:
python3 ok --submit
Readings: You might find the following references useful:
Newton's Method
Q1
Implement intersect
, which takes two functions f
and g
and their
derivatives df
and dg
. It returns an intersection point x
, at which
f(x)
is equal to g(x)
.
def intersect(f, df, g, dg):
"""Return where f with derivative df intersects g with derivative dg.
>>> parabola, line = lambda x: x*x - 2, lambda x: x + 10
>>> dp, dl = lambda x: 2*x, lambda x: 1
>>> intersect(parabola, dp, line, dl)
4.0
"""
"*** YOUR CODE HERE ***"
Q2
Newton's method is not guaranteed to find a zero. One way that it can fail is due to a cycle: after applying k
newton updates from an initial guess x
, the result is x
again.
Implement cycle
, which returns an x
near the given guess
for which applying k
Newton updates for the provided function f
and its derivative df
results in x
. The doctests use a function print_updates
which prints out the result of applying k
Newton updates.
Hint: Part of your solution will involve computing the result of applying k
updates from a starting point.
Hint: The provided differentiate
function will find the derivative of a function automatically. The result is approximate, but good enough for this problem.
def print_updates(f, df, x, k, digits=4):
"""Print the first k Newton guesses for a zero of f, starting at x.
>>> print_updates(lambda x: x*x - 2, lambda x: 2*x, 1, 4)
1, 1.5, 1.4167, 1.4142, 1.4142
"""
update = newton_update(f, df)
guesses = [x]
for _ in range(k):
guesses.append(update(guesses[-1]))
print(*[round(guess, digits) for guess in guesses], sep=', ')
def cycle(f, df, k, guess):
"""Find a k-step cycle in Newton's method starting near guess.
>>> f = lambda x: x*x*x - 8*x*x + 17*x - 3
>>> df = lambda x: 3*x*x - 16*x + 17
>>> f(find_zero(f, df, 1)) # Starting at a guess of 1 finds a zero
0.0
>>> print_cycle = lambda k, x: print_updates(f, df, cycle(f, df, k, x), k)
>>> print_cycle(3, 4.2) # A 3-step cycle starting near 4.2
4.2123, 3.7175, 4.7112, 4.2123
>>> print_cycle(3, 3.7) # A 3-step cycle starting near 3.7 (the same cycle)
3.7175, 4.7112, 4.2123, 3.7175
>>> print_cycle(5, 4) # A 5-step cycle starting near 4
4.003, 3.0234, 3.7591, 5.0564, 4.4548, 4.003
"""
"*** YOUR CODE HERE ***"