Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started