Write a Python program to re-implement the secant method using recursion. Use yo
ID: 3730738 • Letter: W
Question
Write a Python program to re-implement the secant method using recursion. Use your program to find a root of the function with tolerance 0.0001 Requirement: define a function with name "Secant recursion Q3 ### Solution to Question 3 f(x)2cos(x) 2sin(z). r + def Secant.recursion.Q30 codes calls Secant.recursionQ30 recursively. For the starting points in secant's method, one may use 1 and 1.02. Note for efficiency, it is important that in each recursion call, only 1 function evaluation f(x) is done.Explanation / Answer
Solution:
code:
from math import cos, sin
tolerance = 0.0001
def non_poly_func(x):
return x**2 + cos(x)**7 + 2*sin(x)
def non_poly_func_der(x):
return 2*x - 7 * cos(x)**6 * sin(x) + 2 * cos(x)
def Secant_recurssion_Q3(f, x0, x1, tolerance, num_steps):
initial_x0 = x0
initial_x1 = x1
y0 = f(x0)
y1 = f(x1)
if(abs(y1) <= tolerance):
print (num_steps, "steps and approx. root is ", x1)
return x1
else:
# y1 / (x1 - x0) / (y1 - y0)
x2 = x1 - y1 * (x1-x0) / (y1-y0)
# updates
x0 = x1
x1 = x2
#y0 = f(x0) # <-- important that this NOT needed (to save running-time)
y0 = y1
y1 = f(x1)
num_steps = num_steps + 1
return Secant_recurssion_Q3(f, x0, x1, tolerance, num_steps)
Root = Secant_recurssion_Q3 (non_poly_func, 1, 1.02, tolerance, 0)
print(Root)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.