-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewton.py
108 lines (84 loc) · 2.41 KB
/
Newton.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import numpy as np
import matplotlib.pyplot as plt
from tictoc import *
from plot import plot
#from RFA import newton
def newton(f,fp,x0,TOL,NMAX):
#INPUT : f , fp , x0 , TOL , NMAX
# f : function / polynomial
# fp : derivative of f
# x0 : initial guess
# TOL : tolerance value
# NMAX : maximum number of iterations
#Approximates root using Newton's Method
#Recursive Program
#Print Header
print('--------------------------------------------------------------------------')
print('iter \t\t xi \t\t correction \t\t rdiff ')
print('--------------------------------------------------------------------------')
# initiate values for iteration loop:
rdiff = 1
xi = x0
counter = 0
while rdiff > TOL and counter < NMAX:
# get the number of necessary iterations at that particular x0
# compute relative difference
rdiff = np.abs(f(xi)/fp(xi)/xi)
# next xi:
x1 = xi - f(xi)/fp(xi)
N.append(counter+1)
f_x.append(f(x1))
# print iteration data:
print('%i \t %15.12f \t %15.12f \t %15.12f' % (counter+1, x1, np.abs(f(xi)/fp(xi)), rdiff))
# prepare for the next iteration:
xi = x1
counter += 1
if counter == NMAX:
print("MAX NUMBER OF ITERATIONS REACHED!")
print('Approximaiton to the Root after max iterations is : ' , x1)
exit()
print('------------------------------------------------------------------------')
print('Root Found: ', x1)
f1 = lambda x: x**3 - 3*(x**2) - x + 9
f1p = lambda x: 3*(x**2) - 6*x -1
f2 = lambda x: np.exp(x)*(x**3 - 3*(x**2) - x + 9)
f2p = lambda x: np.exp(x)*((x**3 - 3*(x**2) - x + 9)+(3*(x**2) - 6*x -1))
f3 = lambda x: x**3 - 2*x + 2
f3p = lambda x: 3*(x**2) - 2
x01 = -2
x02 = 5
N = []
f_x = []
tic()
newton(f3,f3p,0,1.e-10,1000)
toc()
plot(N,f_x,'b',3)
plt.show()
N = []
f_x = []
tic()
newton(f1,f1p,x01,1.e-10,1000)
toc()
plot(N,f_x,'b',1)
N = []
f_x = []
tic()
newton(f1,f1p,x02,1.e-10,1000)
toc()
plot(N,f_x,'r',1)
plt.legend(['x01 = -2','x02 = +5'])
plt.show()
N = []
f_x = []
tic()
newton(f2,f2p,x01,1.e-10,1000)
toc()
plot(N,f_x,'b',2)
N = []
f_x = []
tic()
newton(f2,f2p,x02,1.e-10,1000)
toc()
plot(N,f_x,'r',2)
plt.legend(['x01 = -2','x02 = +5'])
plt.show()