Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Python Exp3-Semester7.ipynb - Colab

Uploaded by

Status king
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)
15 views

Python Exp3-Semester7.ipynb - Colab

Uploaded by

Status king
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

10/09/2024, 21:59 python exp3-semester7.

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

Bisection Method Root: 1.5213804244995117

# Newton-Raphson Method
def newton_raphson_method(func, func_derivative, initial_guess, tol=1e-6, max_iter=100):
x = initial_guess
iteration = 0

while abs(func(x)) > tol and iteration < max_iter:


x = x - func(x) / func_derivative(x)
iteration += 1

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

root_newton = newton_raphson_method(func_newton, func_newton_derivative, initial_guess=1.5)


print(f"Newton-Raphson Method Root: {root_newton}")

Newton-Raphson Method Root: 1.4142135623746899

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

You might also like