Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a function called newton_ab which takes 6 input parameters a, b, c, x0, max_iter (default value 10 ) and epsilon (default value 1e-10)
Write a function called newton_ab which takes 6 input parameters a, b, c, x0, max_iter (default value 10 ) and epsilon (default value 1e-10) and implements Newton's method with initial guess x and returns an approximation of a solution of the equation f(x) = 0 where f(x) = x + ax + bx + c The value_max_iter is the maximum number of iterations of Newton's method to implement. The value epsilon is the stopping criteria: f(xn) < . Use may use the function newton defined below. ]: def newton (f, Df, x0,epsilon, max_iter): xn = x0 for n in range(0,max_iter): fxn = f(xn) if abs (fxn) < epsilon: print('Found solution after',n, 'iterations. ') return xn Dfxn = Df(xn) if abs (Dfxn) < 1e-12: print('Zero derivative. No solution found.') return None xn = xn - fxn/Dfxn print('Exceeded maximum iterations. No solution found. ') return None # YOUR CODE HERE "Check function accepts the right number of input parameters. (1 mark)" assert type (newton_ab(2,-1,-1,1)) == float, "Return value should be a float." assert type(newton_ab (2,-1,-1,1,10)) == float, "Return value should be a float." assert type (newton_ab(2,-1,-1,1,10,1e-4)) == float, "Return value should be a float." print("Problem 3 Test 2: Success!") "Check function returns the correct datatype when there is a zero derivative. (1 mark)" assert newton_ab(-2,-1,1,1,25,0.001) == None, "Return value should be None when f'(x) print("Problem 3 Test 3: Success!") = 5x^4 - 4x "Check function returns the correct values. This cell contains hidden tests. (2 marks)" assert abs (newton_ab(2, -1,-1,1) - 0.8421756416969274) < 1e-12 assert abs (newton_ab(5,4,-3,1,10,1e-12) - 0.4691683039523567) < le-12 print("Problem 3 Test 5: Success!") - 1 and x0 = 1." "Check function returns the correct datatype when exceed maximum iterations. (1 mark)" assert newton_ab (2,-1,-1,10, 2, 1e-12) == None, "Return value should be None when maximum iterations is exceeded." print("Problem 3 Test 4: Success!")
Step by Step Solution
★★★★★
3.44 Rating (163 Votes )
There are 3 Steps involved in it
Step: 1
Solution for the above question is def newtonaba b x0 maxiter10 eps1e10 fx x5 ax b f lambda x x 5 ax ...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