Sure, here are the code snippets for Newton-Raphson method and False Position method in Python:
Newton-Raphson method:
```
def newton_raphson(f, f_prime, x0, tol, max_iter):
x = x0
for i in range(max_iter):
fx = f(x)
if abs(fx) < tol:
return x
f_prime_x = f_prime(x)
if f_prime_x == 0:
return "Derivative is zero. Cannot continue."
x = x - fx / f_prime_x
return "No root found within max iterations."
#Example usage:
f = lambda x: x**2 - 4
f_prime = lambda x: 2*x
x0 = 3
tol = 1e-5
max_iter = 100
root = newton_raphson(f, f_prime, x0, tol, max_iter)
print("Root found by Newton-Raphson method:", root)
```
False Position method:
```
def false_position(f, a, b, tol, max_iter):
if f(a) * f(b) >= 0:
return "Cannot find root in the given interval."
for i in range(max_iter):
c = (a*f(b) - b*f(a)) / (f(b) - f(a))
if f(c) == 0 or abs(f(c)) < tol:
return c
elif f(c) * f(a) < 0:
b = c
else:
a = c
return "No root found within max iterations."
#Example usage:
f = lambda x: x**3 - x**2 - x - 1
a = 1
b = 2
tol = 1e-5
max_iter = 100
root = false_position(f, a, b, tol, max_iter)
print("Root found by False Position method:", root)
```
write the codes of newton raphson method,false position method in numerical analysis
1 answer