Question
Write a program that uses the bisect method to find the root of a function. (Must Be in Python) Translate the pseudo code given into
Write a program that uses the bisect method to find the root of a function. (Must Be in Python)
Translate the pseudo code given into Python using a while loop.
Pseudo Code:
def f(x):
return 3*x**3 - 4*x**2 + 2*x - 5
INPUT: Function f, endpoint values a, b, tolerance TOL, maximum iterations NMAX CONDITIONS: a < b, either f(a) < 0 and f(b) > 0 or f(a) > 0 and f(b) < 0 OUTPUT: value which differs from a root of f(x)=0 by less than TOL
solutionFound = False N 1 While N NMAX # limit iterations to prevent infinite loop c (a + b)/2 # new midpoint If f(c) = 0 or (b a)/2 < TOL then # solution found Output(N,c) solutionFound = True exit loop EndIf N N + 1 # increment step counter If sign(f(c)) = sign(f(a)) then a c else b c # new interval EndWhile If solutionFound is not True then Output("Method failed.") # max number of steps exceeded EndIf """ print("a = left bracket point (x value)") print("b = right bracket point (x value)") print("TOL = how close to true answer is acceptable") print("NMAX = maximum allowable iterations") print()
a, b, TOL, NMAX = eval(input("Enter a, b, TOL, NMAX: "))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started