Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CSCI 2202 Python Programming for Scientists Lab 9

2022


This lab focuses on modular programming, using functions.  The application we cover is locating the roots of equations.


(1) Implement a function bisection, that can operate on an arbitrary function. Your pro- gram should accept two endpoints (x0, x1), a tolerance limit (e), and a function (f - to find the roots of) as input parameters.  It should output the final approximation to the root and the number of iterations it took to find the root. The program should make sure that the initial interval is acceptable for this method (i.e.  it should be greater than the tolerance (e). Recall that the max. iterations n to achieve an error



bound of:

x1 - x0


ln (x1 - x0 ) - ln(2 . e)

ln 2


The  algorithm for the  bisection  method for finding roots,  is in the notes online. Note1: Define Python functions for function evaluations in the problems:

Note2: You can return two values from a python function:

Use return  rootApprox,  iterCount as the final line in the function. Pick up both values in the calling code: xr, numIter  = bisection(a, b,  tol,  f).

(a) Use the bisection(f,  x0,  x1,  tol, m) to compute  (i) the positive root of f (x) = x2  - x - 1.  The positive root of this equation is known to lie in the interval [1,2]. Use a tolerance of f 10-8 .  (ii) Repeat this exercise for the negative root:. Search in [-1, 0]. (The solution to (i) is the Golden Ratio: Φ the limit of the ratio of consecutive Fibonacci numbers. The solution to (ii) is known to be -1/Φ).

(b) Test your program by plotting and finding the roots of f (x) = ln(2 + x) - ,x using [x0 , x1] = [0, 10].  Plot the function over [0, 10].  Use a tolerance of 10-8 . Print x,  | f (x) | and the number of iterations required for convergence.

(c) Use your program to compute the roots of f1 (x) = x3 - 3x - 1 on [0, 2], f2 (x) = x3 - 2 sin(x) on [0, 2], f3  = tan(x) - 2x on [0, 2].

Start by plotting the functions to find a reasonable interval to bracket the root and print x,  | f (x) | and the number of iterations required for convergence.




l


2

(2) If the function (that you want to find the roots of) is, in addition to being con- tinuous, also differentiable, then we have a very fast method for root finding that arises from a simple idea:

At any point (x0 , f (x0 )) of the curve, the function can be approximated reason- ably, in the vicinity of x0  by the straight line representing the tangent to the function. Newton’s method (a.k.a. Newton-Raphson Method), exploits this f (x0 )



f/ (xk )





when the loop terminates, report x1  as the approximation and the number of

iterations.   Print the number of function evaluations:   (each time  f  or f   is evaluated)

The newton-raphson function should accept f, f/ , x0 , e as input parameters and should output the approximation and the number of iterations.


(2a) Repeat 1(b) and 1(c)(for f2  & f3 ) for Newton’s method. Recall, for New- ton’s method you need the derivative of the function. For f (x) = ln(x+2) - ,x; df/dx(x) = 1/(x + 2) - 1/(2,x).  Pick x0  carefully.  Notice the change in the number of iterations.