Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses the bisect method to find the root of a function. (Mus

ID: 3834462 • Letter: W

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 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: "))

Explanation / Answer

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module
import cmath

a = 1
b = 5
c = 6

# To take coefficient input from the users
# a = float(input('Enter a: '))
# b = float(input('Enter b: '))
# c = float(input('Enter c: '))

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))

You can use numpy.linalg.solve:

7down voteaccepted

You can use numpy.linalg.solve:

      import numpy as np  a = np.array([[2, -4, 4], [34, 3, -1], [1, 1, 1]])  b = np.array([8, 30, 108])  x = np.linalg.solve(a, b)  print x   
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote