Python Exp3-Semester7.ipynb - Colab
Python Exp3-Semester7.ipynb - Colab
ipynb - Colab
# Bisection Method
def bisection_method(func, a, b, tol=1e-6, max_iter=100):
if func(a) * func(b) > 0:
raise ValueError("The function values at the interval endpoints must have opposite signs.")
iteration = 0
while (b - a) / 2 > tol and iteration < max_iter:
c = (a + b) / 2
if func(c) == 0:
return c # Found exact root
elif func(c) * func(a) < 0:
b = c
else:
a = c
iteration += 1
return (a + b) / 2
# Example usage:
def func(x):
return x**3 - x - 2 # Example: f(x) = x^3 - x - 2
root = bisection_method(func, 1, 2)
print(f"Bisection Method Root: {root}")
# Newton-Raphson Method
def newton_raphson_method(func, func_derivative, initial_guess, tol=1e-6, max_iter=100):
x = initial_guess
iteration = 0
return x
# Example usage:
def func_newton(x):
return x**2 - 2 # Example: f(x) = x^2 - 2
def func_newton_derivative(x):
return 2 * x # Derivative: f'(x) = 2x
https://colab.research.google.com/drive/11OI8Yoi8soWPNY2DScpAqJEU9CVgOsBn#scrollTo=2yko1Jj-w7jI&printMode=true 1/2
10/09/2024, 21:59 python exp3-semester7.ipynb - Colab
https://colab.research.google.com/drive/11OI8Yoi8soWPNY2DScpAqJEU9CVgOsBn#scrollTo=2yko1Jj-w7jI&printMode=true 2/2