0% found this document useful (0 votes)
13 views2 pages

Code

Uploaded by

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

Code

Uploaded by

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

def root_safe_window(polynomial, x_min, x_max, tolerance):

def evaluate_poly(coeffs, x):

return sum(c * (x**i) for i, c in enumerate(coeffs))

def has_sign_change(f_a, f_b):

return f_a * f_b < 0

# Initialize

intervals = [(x_min, x_max)]

roots = []

# Divide intervals and find root-safe intervals

while intervals:

a, b = intervals.pop(0)

f_a = evaluate_poly(polynomial, a)

f_b = evaluate_poly(polynomial, b)

if has_sign_change(f_a, f_b):

# Bisect interval

while abs(b - a) > tolerance:

m = (a + b) / 2

f_m = evaluate_poly(polynomial, m)

if has_sign_change(f_a, f_m):

b, f_b = m, f_m

elif has_sign_change(f_m, f_b):

a, f_a = m, f_m

else:

break # No sign change, stop early

roots.append((a + b) / 2)
else:

continue # Root-safe interval

return roots

# Example use case:

coeffs = [1, 0, -5, 0, 6] # Coefficients for x^4 - 5x^2 + 6

roots = root_safe_window(coeffs, -10, 10, 1e-6)

print("Roots:", roots)

You might also like