0% found this document useful (0 votes)
4 views3 pages

Bisection New

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Bisection New

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import math

""" MICHAEL JOHN DENQUE'S WORKS,

CRDT: ONLINE TOTURIAL & SOURCE

"""

def bisection_method(func, lower, upper, max_iter, tol):

#TABLE

print(f"{'i':<17}{'xl':<20}{'xu':<20}{'xr':<20s}{'ea%':<27}")

print('-'*70)

for i in range(max_iter):

midpoint = (lower + upper) / 2 # Calculate midpoint/xr

f_mid = func(midpoint) #f(xr)

# Calculate the error/ea

error = abs((midpoint - lower) / midpoint) * 100

# Print the current iteration's results

print(f"{i + 1:<15}{lower:<20.5f}{upper:<20.5f}{midpoint:<20.5f}{error:<15.5f}")

if error <= tol: #KANI RAY GI DUNGAG

break

if f_mid == 0:

return midpoint
# Narrow down the interval

if func(lower) * f_mid < 0:

upper = midpoint # Root is in the lower half

else:

lower = midpoint # Root is in the upper half

return (lower + upper) / 2 # Return the best estimate after max iterations

# Example usage

if __name__ == "__main__":

# Input function as a string

while True:

func_input = input("Enter the function of x or 'q' to quit: ")

if func_input.lower() == 'q':

print("\n OKAY BYEE!!!")

break

else:

# Define the function using eval

def func(x):

return eval(func_input)

# Input lower and upper limits

lower = float(input("Enter the lower limit: "))

upper = float(input("Enter the upper limit: "))

tol = float(input("Enter the Tolerance Percentage: "))


# Input maximum iterations

max_iterations = int(input("Enter the maximum number of iterations: "))

try:

root = bisection_method(func, lower, upper, max_iterations, tol)

print(f"\nThe root is approximately: {root:.5f}")

except ValueError as e:

print(e)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy