Develop a python program to find the different solutions, i.e. zero crossings, of a polynomial function using Newton-Raphson’s method over a given range. To do this we will develop three functions: the first should be a function to evaluate the polynomial at a given value of the independent variable; the second function would evaluate the derivative of the polynomial at a given value of the independent variable; finally, the third function would implement Newton-Raphson’s (NR) method to determine a zero of the polynomial in the neighborhood of a specified point where we believe that a solution exists. You would repetitively use this NR function to find solutions starting from the proposed neighborhood values. Test your program on the following polynomials: ?*3 − 9?*2 + 24? − 20 = 0 for ? being in the neighborhood of 0, 2.5, and 6, and ?*3 − 2?*2 − 5? + 6 = 0 for ? being in the neighborhood of -1.5, 0.5, and 2.5. You may want to initially develop the program to be tailored for a third order polynomial. This will make the evaluation and derivative functions almost trivial, i.e. one liners, which is fine. Once the program runs for the two given test data, you may contemplate the challenge of making it more general, capable of working with a polynomial of any order. An additional challenge would be to use your evaluation function to generate multiple values and to select from these the ones that seem closest to a solution and put them in a list of neighborhood values. Note every time the function changes sign between two values, this is an indication that there is a zero of the function between these values.
Python code for ?*3 − 9?*2 + 24? − 20 = 0 in the neighborhood of 0, 2.5, and 6.
def func( x ):
return x * x * x - 2 *x * x - 5*x + 6
# Derivative of the above function
# which is 3*x^x - 2*x
def derivFunc( x ):
return 3 * x * x - 4 * x -5
# Function to find the root
def newtonRaphson( x ):
h = func(x) / derivFunc(x)
while abs(h) >= 0.0001:
h =
func(x)/derivFunc(x)
# x(i+1) = x(i) - f(x) /
f'(x)
x = x - h
print("The value of the root is : ",
"%.4f"% x)
# Driver program to test above
# Initial values assumed
x0 = 0
x1 = 2.5
x2 = 6
newtonRaphson(x0)
newtonRaphson(x1)
newtonRaphson(x2)
Please refer to the screenshot of the code to understand the indentation of the code
Output:
Hope this helped. Please do upvote and if there are any queries please ask in comments section.
Get Answers For Free
Most questions answered within 1 hours.