Question

In: Computer Science

Make a python function that inputs f : a function whose root you wish to find,...

Make a python function that inputs f : a function whose root you wish to find, x0 : an initial guess for the root, x1 : an second guess for the root, and k : a number of iterations, and output and outputs a list of the results of performing the secant method k times starting from x0 and x1. Use your function to check if the secant method has the claimed rate of convergence when applied to f ( x ) = e x + x starting with x 0 = 3 and x 1 = 2

Solutions

Expert Solution

import math
def secant(f,x0,x1,k):
    if f(x0)*f(x1) >= 0:
        print("Secant method fails.")
        return None
    x0_n = x0
    x1_n = x1
    results = []
    for n in range(1, k+1):
        m_n = x0_n - f(x0_n)*(x1_n - x0_n)/(f(x1_n) - f(x0_n))
        f_m_n = f(m_n)
        if f(x0_n)*f_m_n < 0:
            x0_n = x0_n
            x1_n = m_n
        elif f(x1_n)*f_m_n < 0:
            x0_n = m_n
            x1_n = x1_n
        elif f_m_n == 0:
            print("Found exact solution.")
            return m_n
        else:
            print("Secant method fails.")
            return None
        results.append(m_n)
    return results

def f(x): 
    return math.exp(x) * x

print(secant(f, -3, 2, 100))

We get:

Screenshot of the code:


Related Solutions

Using Python, create a function whose inputs are two matrices and whose output is the element...
Using Python, create a function whose inputs are two matrices and whose output is the element wise product of the two matrices.
Find the root of the function f(x) = 8 - 4.5 ( x - sin x...
Find the root of the function f(x) = 8 - 4.5 ( x - sin x ) in the interval [2,3]. Exhibit a numerical solution using Newton method.
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Given the function f(x) on the right solve the following root finding questions: a) Find a...
Given the function f(x) on the right solve the following root finding questions: a) Find a positive root (x > 0) of f(x) using the Bisection Method . b) Find a negative root (x < 0) of f(x) using the Bisection Method. c) Find a positive root (x > 0) of f(x) using the False Position Method. d) Find a negative root (x < 0) of f(x) using the False Position Method. Find your initial Bracket via Trial-and-Error. Use |...
Use the secant Method to find a root for the function: f(x) = x^3 + 2x^2...
Use the secant Method to find a root for the function: f(x) = x^3 + 2x^2 + 10x -20, with x_0 = 2, and x_1 = 1.
Using Python, write a program that finds a root of function f(x) = x^2-4x+3 on interval...
Using Python, write a program that finds a root of function f(x) = x^2-4x+3 on interval [2,5] using bisection search method. Recall that r is a root of a function if f(r) = 0. Output the number of iterations it took your algorithm to find a root.
Question 1 f is a function whose domain is (0,∞) and whose first derivative is f...
Question 1 f is a function whose domain is (0,∞) and whose first derivative is f ' (x) = (4 − x)x -3 for x > 0 a) Find the critical number(s) of f. Does f have a local minimum, a local maximum, or neither at its critical number(s)? Explain. b) On which interval(s) if f concave up, concave down? c) At which value(s) of x does f have a point of inflection, if any? Question 2 A rectangular box...
Using Newton-Raphson method, find the complex root of the function f(z) = z 2 + z...
Using Newton-Raphson method, find the complex root of the function f(z) = z 2 + z + 1 with with an accuracy of 10–6. Let z0 = 1 − i. write program c++ or matlab
Using the bisection method, find the root of the following function: f(x)=cos(2x) - x Use the...
Using the bisection method, find the root of the following function: f(x)=cos(2x) - x Use the following initial values: xl=0 and xu=2 NOTE: Perform only 3 iterations.
Consider the root of function f(x) = x 3 − 2x − 5. The function can...
Consider the root of function f(x) = x 3 − 2x − 5. The function can be rearranged in the form x = g(x) in the following three ways: (a) x = g(x) = x3 − x − 5 (b) x = g(x) = (x 3 − 5)/2 (c) x = g(x) = thirdroot(2x + 5) For each form, apply fixed-point method with an initial guess x0 = 0.5 to approximate the root. Use the error tolerance = 10-5 to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT