lab7-extra

Relevant Socratica video:

newton(f, x, feps, maxit)

Implement a function newton(f, x, feps, maxit) which takes a function f(x) and an initial guess x for the root of the function f(x), an allowed tolerance feps and the maximum number of iterations that are allowed maxit. The newton function should use the following Newton-Raphson algorithm (here in pseudo code):

while |f(x)| > feps, do
   x = x - f(x) / fprime(x)

where fprime(x) is an approximation of the first derivative \(\frac{\mathrm{d}f}{\mathrm{d}x}(x)\) at position \(x\).

You should use the derivative function derivative that you have implemented before to compute fprime(x).

If maxit or fewer iterations are necessary for |f(x)| to become smaller than feps, then the value for x should be returned:

In [ ]: def f(x):
   ....:     return x ** 2 - 2
   ....:

In [ ]: newton(f, 1.0, 0.2, 15)
Out[ ]: 1.4166666666783148

In [ ]: newton(f, 1.0, 0.2, 15) - math.sqrt(2)
Out[ ]: 0.002453104305219611

In [ ]: newton(f, 1.0, 0.001, 15)
Out[ ]: 1.4142156862748523

In [ ]: newton(f, 1.0, 0.001, 15) - math.sqrt(2)
Out[ ]: 2.1239017571339502e-06

In [ ]: newton(f, 1.0, 0.000001, 15) - math.sqrt(2)
Out[ ]: 1.5949463971764999e-12

If more than maxit iterations are necessary for the function newton, then the newton function should raise the RuntimeError exception with the message: Failed after X iterations where X is to be replaced with the number of iterations:

In [23]: def g(x):
   ....:     return math.sin(x) + 1.1  # has no root!
   ....:

In [24]: newton(g, 1.0, 0.02, 15)
Traceback (most recent call last):

  File "<ipython-input-6-0a9db3f67256>", line 1, in <module>
    newton(g, 1.0, 0.02, 15)

  File "..lab7.py", line 16, in newton
    raise RuntimeError(f"Failed after {maxit} iterations")

RuntimeError: Failed after 15 iterations

The relevant line of Python to be executed if the number of maxit iterations is reached, is raise RuntimeError(f"Failed after {maxit} iterations")

Hint

Python provides a while loop that can be used in the implementation. Here is an example:

In [ ]: import time

In [ ]: a = 10

In [ ]: while a > 0:
    ...:     print(f"Countdown: a={a}")
    ...:     time.sleep(1)  # wait for 1 second
    ...:     a = a - 1
    ...: print("Done")

Please include the extra tasks in your file lab7.py and submit as Computing lab7 assignment.

Back to lab7.

End of lab7-extra.