Skip to content

Commit 2b0bc1c

Browse files
committed
newton and quasi-newton convergence experiments
1 parent 44c4651 commit 2b0bc1c

6 files changed

+570
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

interp.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import numpy as np
2+
import numpy.linalg as la
3+
import matplotlib.pyplot as plt
4+
5+
6+
def driver():
7+
8+
9+
f = lambda x: np.exp(x)
10+
11+
N = 3
12+
''' interval'''
13+
a = 0
14+
b = 1
15+
16+
17+
''' create equispaced interpolation nodes'''
18+
xint = np.linspace(a,b,N+1)
19+
20+
''' create interpolation data'''
21+
yint = f(xint)
22+
23+
''' create points for evaluating the Lagrange interpolating polynomial'''
24+
Neval = 1000
25+
xeval = np.linspace(a,b,Neval+1)
26+
yeval_l= np.zeros(Neval+1)
27+
yeval_dd = np.zeros(Neval+1)
28+
29+
'''Initialize and populate the first columns of the
30+
divided difference matrix. We will pass the x vector'''
31+
y = np.zeros( (N+1, N+1) )
32+
33+
for j in range(N+1):
34+
y[j][0] = yint[j]
35+
36+
y = dividedDiffTable(xint, y, N+1)
37+
''' evaluate lagrange poly '''
38+
for kk in range(Neval+1):
39+
yeval_l[kk] = eval_lagrange(xeval[kk],xint,yint,N)
40+
yeval_dd[kk] = evalDDpoly(xeval[kk],xint,y,N)
41+
42+
43+
44+
45+
46+
''' create vector with exact values'''
47+
fex = f(xeval)
48+
49+
50+
plt.figure()
51+
plt.plot(xeval,fex,'ro-')
52+
plt.plot(xeval,yeval_l,'bs--')
53+
plt.plot(xeval,yeval_dd,'c.--')
54+
plt.legend()
55+
56+
plt.figure()
57+
err_l = abs(yeval_l-fex)
58+
err_dd = abs(yeval_dd-fex)
59+
plt.semilogy(xeval,err_l,'ro--',label='lagrange')
60+
plt.semilogy(xeval,err_dd,'bs--',label='Newton DD')
61+
plt.legend()
62+
plt.show()
63+
64+
def eval_lagrange(xeval,xint,yint,N):
65+
66+
lj = np.ones(N+1)
67+
68+
for count in range(N+1):
69+
for jj in range(N+1):
70+
if (jj != count):
71+
lj[count] = lj[count]*(xeval - xint[jj])/(xint[count]-xint[jj])
72+
73+
yeval = 0.
74+
75+
for jj in range(N+1):
76+
yeval = yeval + yint[jj]*lj[jj]
77+
78+
return(yeval)
79+
80+
81+
82+
83+
''' create divided difference matrix'''
84+
def dividedDiffTable(x, y, n):
85+
86+
for i in range(1, n):
87+
for j in range(n - i):
88+
y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /
89+
(x[j] - x[i + j]));
90+
return y;
91+
92+
def evalDDpoly(xval, xint,y,N):
93+
''' evaluate the polynomial terms'''
94+
ptmp = np.zeros(N+1)
95+
96+
ptmp[0] = 1.
97+
for j in range(N):
98+
ptmp[j+1] = ptmp[j]*(xval-xint[j])
99+
100+
'''evaluate the divided difference polynomial'''
101+
yeval = 0.
102+
for j in range(N+1):
103+
yeval = yeval + y[0][j]*ptmp[j]
104+
105+
return yeval
106+
107+
108+
109+
driver()

interp_system.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
3+
f = lambda x: x*x**2 - y**2
4+
g = lambda x: 3*x*y**2 - x**3 - 1
5+
6+
x,y = 1,1
7+
8+
matrix = [[1/6, 1/18],
9+
[0, 1/6]]
10+
11+
for i in range(1000):
12+
vec = [x,y] - np.matmul(matrix, [f(x), g(y)])
13+
14+
x = vec[0]
15+
16+
y = vec[1]
17+
18+
print(x,y)
19+

mynewtonsystems.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import numpy as np
2+
import math
3+
import time
4+
from numpy.linalg import inv
5+
from numpy.linalg import norm
6+
import matplotlib.pyplot as plt
7+
import sympy
8+
9+
f = lambda x,y: x**2 + y**2 - 4
10+
g = lambda x,y: np.exp(x) + y - 1
11+
12+
x,y = 1,1
13+
14+
J = lambda x,y: [[2*x, 2*y],
15+
[np.exp(x), 1]]
16+
17+
# J_broyden = lambda x,y:
18+
19+
20+
for init in [[1,1], [1,-1], [0,0]]:
21+
22+
print('\nINITIAL CONDIITONS', init)
23+
24+
x = init[0]
25+
y = init[1]
26+
27+
# Newton's method
28+
29+
print('\nVANILLA NEWTON\n', x, y)
30+
31+
32+
try:
33+
34+
for i in range(20):
35+
vec = [x,y] - np.matmul(inv(J(x,y)), [f(x,y), g(x,y)])
36+
37+
x = vec[0]
38+
39+
y = vec[1]
40+
41+
print(x,y)
42+
43+
except:
44+
45+
print('Error')
46+
47+
48+
49+
50+
# Lazy Newton
51+
52+
x = init[0]
53+
y = init[1]
54+
55+
56+
57+
print('\nLAZY\n', x, y)
58+
59+
# calc J once
60+
61+
J_ = J(x,y)
62+
63+
64+
try:
65+
66+
for i in range(50):
67+
vec = [x,y] - np.matmul(inv(J_), [f(x,y), g(x,y)])
68+
69+
x = vec[0]
70+
71+
y = vec[1]
72+
73+
print(x,y)
74+
75+
except:
76+
77+
print('Error')
78+
79+
80+
81+
x,y = init
82+
83+
print('\nBROYDEN\n', x, y)
84+
85+
for i in range(50):
86+
87+
vec = [x,y] - np.matmul(inv(J_broyden(x,y)), [f(x,y), g(x,y)])
88+
89+
x = vec[0]
90+
91+
y = vec[1]
92+
93+
print(x,y)

0 commit comments

Comments
 (0)