-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewton's Method.py
38 lines (30 loc) · 1.61 KB
/
Newton's Method.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Root Finding Algorithms in Numerical Methods, Newton's Method
#Author: Ömer Bera Dinç
import numpy as np
import math
# Since I did not write the iteration with loops, I defined the number of iterations with self and then increased it as I called the function. So I needed class structure.
class numericalmethods:
def __init__(self):
#self.n = Iteration variable
self.n = 0
# defining my equation here
self.f = lambda x: x**(1/3) + np.log(x)
# defining the derivative of my equation here
self.f_prime = lambda x: 1/(3*(x**(2/3))) + 1/x
# calling my function, it takes 4 variables, equation (self.f), derivative of equation (self.f_prime), initial point
estimate = self.my_newton(self.f, self.f_prime, 1, 10**-7)
print("estimated root =", estimate)
def my_newton(self, f, df, x0, tol):
# output is an estimation of the root of f
# using the Newton Raphson method
# recursive implementation
self.n += 1
print("--------------")
if abs(self.f(x0) / self.f_prime(x0)) < tol:
print("n: ", self.n, "xn: ", x0, "f(xn): ", self.f(x0), "f'(xn): ", self.f_prime(x0), " xn+1: ", x0 - f(x0)/df(x0), "|xn - xn+1| ", abs(f(x0)/df(x0)) )
return x0
else:
print("n: ", self.n, "xn: ", x0, "f(xn): ", self.f(x0), "f'(xn): ", self.f_prime(x0), " xn+1: ", x0 - f(x0)/df(x0), "|xn - xn+1| ", abs(f(x0)/df(x0)) )
return self.my_newton(f, df, x0 - f(x0)/df(x0), tol)
if __name__ == "__main__":
numericalmethods()