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
Input Arguments
Your function should take the following as input:
Functions for evaluating and its derivative
Initial solution
Maximum number of iterations and stopping tolerance.
Output Arguments
Your function should return the following as output:
The finite sequence of solutions dots, where is the solution when the algorithm is terminated.
The sequence of absolute differences in consecutive iterates dots,
The number of iterations performed.
The function below gives the general form of Newton's Method but is missing code for executing several steps, eg calculating from updating the residuals You will need to modify the given code to include these steps.
: def Newton nmax, tol:
n
Performs NewtonRaphson Method to calculate a root of function
: function for evaluating
df: function for evaluating
: 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 # Number of iterations is
tol # Initialize function value to be greater than tol.
errs npones # Initial vector of errors.
# Update iterate until a root is found or max # of iterations are performed. while iter nmax and errsiter tol:
# Calculate function value and derivative value.
iter
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
