Question: Modify the following code to obtain a Python function for performing Newton's method for a given function f . Input Arguments Your function should take

Modify the following code to obtain a Python function for performing Newton's method for a given function f.
Input Arguments
Your function should take the following as input:
Functions for evaluating f(x) and its derivative f'(x).
Initial solution x0.
Maximum number of iterations and stopping tolerance.
Output Arguments
Your function should return the following as output:
The finite sequence of solutions {x0,x1,dots,xk}, where xk is the solution when the algorithm is terminated.
The sequence of absolute differences in consecutive iterates {|x1-x0|,|x2-x1|,dots,|xk-xk-1|}.
The number of iterations performed.
The function below gives the general form of Newton's Method but is missing code for executing several steps, e.g., calculating xk from xk-1, updating the residuals |xk-xk-1|. You will need to modify the given code to include these steps.
[]: def Newton(f,df,x0, nmax, tol):
""n"
Performs Newton-Raphson Method to calculate a root of function f.
f : function for evaluating f
df: function for evaluating f'
x0 : initial iterate.
nmax: maximum number of iterates to perform.
tol: stopping tolerance
return sequence of approximate roots and number of iterations performed.
""n
# Import numpy.
import numpy as np
# Initialization.
iter =0 # Number of iterations is 0.
fx= tol +1 # Initialize function value to be greater than tol.
errs = np.ones (nmax+1) # Initial vector of errors.
# Update iterate until a root is found or max # of iterations are performed. while (iter nmax and errs[iter]> tol):
# Calculate function value and derivative value.
iter
 Modify the following code to obtain a Python function for performing

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!