Question: Both Exercises are needed in Python. I went ahead and added both the bisection and secant method functions under each exercise. I keep running across

Both Exercises are needed in Python.
I went ahead and added both the bisection and secant method functions under each exercise. I keep running across errors when I try to implement my code so please help.
EXERCISE 13.2
Define a lambda function that represents a cubic polynomial:y(x)=x^3-x-2x
Use this lambda function to generate y values for x values ranging from 0to 3(inclusive)with an increment of 0.1.
Use the bisection_method function defined above to solve the root in the braket of [0,3]with the default stopping criteria value.
Create a line plot to visualize the cubic function.
Add appropriate labels for the x-axis and y-axis, a title for the plot, and a grid for better readability.
Mark the root in the plot.
#EXAMPLE
def bisection_method(f, a, b, es=1e-5):
import numpy as np
i=0
c=(a+b)/2 #first guess
ea=100
if f(a)*f(b)>0:
return 'initial interval does not include solution'
#%% iterative
while ea>es:
if f(a)*f(c)>0:
a=c
else:
b=c
i=i+1
old=c #save old guess
c=(a+b)/2 #new guess
ea=abs((c-old)/c)*100 #relative error
print(c,ea,i)
return c
print('The root lies at', c)
EXERCISE 13.3
Define a lambda function that represents a cubic polynomial:y(x)=x^3-x-2x
Use the lambda function to generate y values for x values ranging from 0to 3(inclusive)with an increment of 0.1.
Use the secant_method function defined above to solve the root in the braket of [0,3]with the default stopping criteria value.
Create a line plot to visualize the cubic function.
Add appropriate labels for the x-axis and y-axis, a title for the plot, and a grid for better readability.
Mark the root in the plot.
#EXAMPLE
def secant_method(f, x0, x1, es=1e-6, maxiter=100):
# Implements the Secant Method to find the root of a function f.
# Parameters:
# f : function, the mathematical function for which to find the root
# x0, x1 : float, initial guesses
# es : float, tolerance for convergence (default: 1e-6)
# max_iter : int, maximum number of iterations (default: 100)
# Returns:
# x2 : float, the approximated root
#initialze
ea=100
i=0
while ea>es and i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!