Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code that is a precursor to this question is seen after the question. from math import sqrt from scipy.optimize import newton,bisect #the NR method

The code that is a precursor to this question is seen after the question.

image text in transcribed

from math import sqrt from scipy.optimize import newton,bisect #the NR method from scipy #i have also imported bisection for comparison

#constants maxit = 50 # maximum number of iterations tol = 0.0001 #default tol eps = 1 #initialize epsilon, any value OK as long as greater than the tolerance

#function and its derivative f = lambda x : -0.9*x**2 + 1.7*x + 2.5 # define function fderiv = lambda x : -1.8*x+1.7 #define derivative of f

#you could also find this with sympy ! Try it!

initial_guess=10 # initial guess, try changing this

#since i'd like to keep the values of x at each iteration #I will make a list and update it with each new value

x = [initial_guess] #initialize list with the guess

i = 0;

while eps>tol and i

#while loop has ended if eps>tol: print('Maximum number or iterations reached, cannot find any root')

#finding the exact values for the roots of the quadratic delt = 1.7**2-4*(-0.9)*2.5 rootcalc1 = (-1.7+sqrt(delt))/(2*-0.9); rootcalc2 = (-1.7-sqrt(delt))/(2*-0.9);

#now for the NR method from scipy #lets see what the variables are for this function #note : we can use the name of the function directly since it has been imported #so no need to type scipy.optimize.newton #help(newton) (root_NR, result_NR)=newton(f,initial_guess,fprime=fderiv,tol=1e-9,full_output=True) # the NR method with the derivative (root_bisect, result_bisect)=bisect(f,0,initial_guess,xtol=1e-9,full_output=True)#xtol is 2e-12 by default

print('The roots from each method and the number of iterations are') print(f'roots from using the quadratic formula are x1= {rootcalc1} x2={rootcalc2}') print(f'x from our NR = {x[i]:.10f} \t number of iterations = {i+1}') print(f'x from scipy NR = {root_NR:.10f} \t number of iterations = {result_NR.iterations}') print(f'x from scipy bisection = {root_bisect:.10f} \t number of iterations = {result_bisect.iterations}')

a)Now, write a function called myNR to implement the Newton-Raphson algorithm to find the root of a given function. For this problem, you are writing a function that will attempt to find the root of an arbitrary user-specified function. Your function should accept the following inputs: i) the name of the function; ii) the derivative of that function; and, iii) an initial guess at the root. For example, if myfun(x) and myfund(x) are functions that return, respectively, the value of the function and its derivative at x, then I should be able to call your function as myNR (myfun, myfund, x0). Use approximate relative error to test whether the algorithm has converged. Your answer should include: i) Python code with appropriate comments; ii) output showing that the following test functions solves correctly. Test your code by finding a root of the following function using an initial guess of -2 f(x) = x3 11x4 + 43x3 73x2 + 56x 16 b) Modify your function to implement the modified Secant algorithm when the derivative is not supplied. This only requires a change in how you obtain the derivative: in the Secant algorithm, it is estimated numerically as f'(x) = f(xi) f(x;-1) Li-Ii-1 Note, the Secant algorithm requires two initial guesses of x; in the modified Secant algorithm the second guess is obtained by taking a small step, say 1%, above or below the guess supplied. a)Now, write a function called myNR to implement the Newton-Raphson algorithm to find the root of a given function. For this problem, you are writing a function that will attempt to find the root of an arbitrary user-specified function. Your function should accept the following inputs: i) the name of the function; ii) the derivative of that function; and, iii) an initial guess at the root. For example, if myfun(x) and myfund(x) are functions that return, respectively, the value of the function and its derivative at x, then I should be able to call your function as myNR (myfun, myfund, x0). Use approximate relative error to test whether the algorithm has converged. Your answer should include: i) Python code with appropriate comments; ii) output showing that the following test functions solves correctly. Test your code by finding a root of the following function using an initial guess of -2 f(x) = x3 11x4 + 43x3 73x2 + 56x 16 b) Modify your function to implement the modified Secant algorithm when the derivative is not supplied. This only requires a change in how you obtain the derivative: in the Secant algorithm, it is estimated numerically as f'(x) = f(xi) f(x;-1) Li-Ii-1 Note, the Secant algorithm requires two initial guesses of x; in the modified Secant algorithm the second guess is obtained by taking a small step, say 1%, above or below the guess supplied

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

=+5 How does HRM relate to efforts to increase innovation?

Answered: 1 week ago