Given the equation

\(f(x) = 0.1x^3 - 10\)

  1. Write a Python script to find the root with the bisection method. Try an initial bracket of [-10, 10]
  2. Write a Python script to find the root with the Newton-Raphson method. Try an initial guess of -10. Note the derivative of our function is \(f'(x) = 0.3x^2\).
  3. Write a Python script to find the root with the secant method. Try an initial guess of -10
# Function definition (used for all methods)
def f(x):
    return 0.1 * x**3 - 10

# Set a tolerance to use for all the methods
tol = 1e-14
# Bisection
# f(x) defined previously
# Step 1 - Set lower and upper bounds
xl = -10
xu = 10

# Step 2 - check the bounds are valid
if f(xl)*f(xu) > 0:
    print(f"The function is not guaranteed to have a root between {xl} and {xu}. Try other bounds.")
else:
    while True: # do what's in this while block until encountering the "break" statement
        # Step 3 - guess the root at the midpoint
        xg = (xl + xu) / 2
        # Step 4 - check the guess
        if abs(f(xg)) <= tol:
            break # done!
        # Step 5 - look left or right?
        if f(xg)*f(xl) <0:
            # look LEFT
            xu = xg  # Step 6 - update search bounds
        else:
            # look RIGHT
            xl = xg  # Step 6 - update search bounds
        # Repeat with new bounds

x_bs = xg
print(f"The Bisection method solution (to tolerance of {tol:.0e}) for the root is {x_bs}")
The Bisection method solution (to tolerance of 1e-14) for the root is 4.6415888336127775
# Newton-Raphson
# f(x) defined previously
# f'(x) definition needed
def dydx(x):
    return 0.3 * x**2

# Step 1 - Set an initial guess
x = -10

while True: # do what's in this while block until encountering the "break" statement
    # Step 2 - check the guess
    if abs(f(x)) <= tol:
        break  # done!
    # Step 3 - "draw" a tangent line
    slope = dydx(x)
    # Step 4 - find x-intercept of tangent
    x = x - f(x)/slope
    # Repeat with new guess

x_nr = x
print(f"The Newton-Raphson solution (to tolerance of {tol:.0e}) for the root is {x_nr}")
The Newton-Raphson solution (to tolerance of 1e-14) for the root is 4.641588833612778
# Secant method
# f(x) defined previously
# A small increment to approximate the derivative is needed
dx = 1e-5

# Step 1 - Set an initial guess
x = -10

while True:  # do what's in this while block until encountering the "break" statement
    # Step 2 - check the guess
    if abs(f(x)) <= tol:
        break  # done!
    # Step 3 - evaluate function nearby
    fxdx = f(x + dx)
    # Step 4 - "draw" a secant line
    slope = (fxdx - f(x))/dx
    # Step 5 - find x-intercept of tangent
    x = x - f(x) / slope
    # Repeat with new guess

x_sm = x
print(f"The Secant method solution (to tolerance of {tol:.0e}) for the root is {x_sm}")
The Secant method solution (to tolerance of 1e-14) for the root is 4.641588833612779

With a solution from all three root-finding methods, we could compare to see which approximation for the root is best by evaluating \(f(x)\) for each guess. But since all the methods worked and were using the same tolerance, there’s little reason to expect the differences to be meaningful. What would be meaningful would be to evaluate which method took the most iterations… Can you modify the code to perform that check?