Fixed-Point Iteration
By Prof. Seungchul Lee
http://iailab.kaist.ac.kr/
Industrial AI Lab at KAIST
http://iailab.kaist.ac.kr/
Industrial AI Lab at KAIST
Source
- By Prof. David J. Malan from Harvard University
- By Prof. Erik Demaine from MIT online lecture
Iteration algorithm
- choose an initial point
- Do the iteration until meeting stopping criteria
Convergence check (or analysis)
Let be the exact solution,
1) Example of
In [1]:
# Computational Thinking on how to calculate cos(x) = x
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
x = np.linspace(-2*np.pi, 2*np.pi, 100)
y = np.cos(x)
plt.figure(figsize = (8,6))
plt.plot(x, y, linewidth = 2)
plt.plot(x, x, linewidth = 2)
# plt.xlim(-2*np.pi, 2*np.pi)
plt.axvline(x=0, color = 'k', linestyle = '--')
plt.axhline(y=0, color = 'k', linestyle = '--')
plt.legend(['cos(x)','x'])
plt.axis('equal')
plt.ylim([-1,1])
plt.show()
In [3]:
# naive approach
x = 0.3
print (np.cos(x))
print (np.cos(np.cos(x)))
print (np.cos(np.cos(np.cos(x))))
print (np.cos(np.cos(np.cos(np.cos(x)))))
print (np.cos(np.cos(np.cos(np.cos(np.cos(x))))))
print (np.cos(np.cos(np.cos(np.cos(np.cos(np.cos(x)))))))
print (np.cos(np.cos(np.cos(np.cos(np.cos(np.cos(np.cos(x))))))))
In [4]:
# better way
x = 0.3
for i in range(24):
tmp = np.cos(x)
x = np.cos(tmp)
print (x)
In [5]:
# better way
x = np.zeros((24, 1))
x[0] = 0.3
for i in range(23):
x[i+1] = np.cos(x[i])
print (x)
In [6]:
# better way
x = 10
for i in range(24):
x = np.cos(x)
print (x)
2) example of
In [7]:
# Use an idea of a fixed point
x = 2
for i in range(10):
x = 2/x
print (x)
In [8]:
x = np.linspace(-3, 3, 100)
y = 2/x
plt.figure(figsize = (8,6))
plt.plot(x, y, linewidth = 2)
plt.plot(x, x, linewidth = 2)
plt.axvline(x=0, color = 'k', linestyle = '--')
plt.axhline(y=0, color = 'k', linestyle = '--')
plt.legend(['2/x','x'])
plt.axis('equal')
plt.ylim([-1,1])
plt.show()
In [9]:
# How to overcome
# Use an idea of a fixed point + kind of *|damping|*
x = 3
for i in range(10):
x = (x + 2/x)/2
print (x)
In [10]:
x = np.linspace(-4, 4, 100)
y = (x + 2/x)/2
plt.figure(figsize = (8,6))
plt.plot(x, y, linewidth = 2)
plt.plot(x, x, linewidth = 2)
plt.axvline(x=0, color = 'k', linestyle = '--')
plt.axhline(y=0, color = 'k', linestyle = '--')
plt.axis('equal')
plt.ylim([-1,1])
plt.show()
Think about why it gives different results.
In [11]:
import numpy as np
import matplotlib.pyplot as plt
# matrix inverse
A = np.array([[4, -1, 1], [4, -8, 1], [-2, 1, 5]])
b = np.array([[7, -21, 15]]).T
x = np.linalg.inv(A).dot(b)
print (x)
This solution only possible for small size problems. There are many iterative methods for large problems.
- In a matrix form
- iteration
In [12]:
# Iterative way
A = np.array(([[0, 1/4, -1/4 ],
[4/8, 0, 1/8],
[2/5, -1/5, 0]]))
b = np.array([[7/4, 21/8, 15/5]]).T
# initial point
x = np.array([[1, 1, 2]]).T
A = np.asmatrix(A)
b = np.asmatrix(b)
x = np.asmatrix(x)
for i in range(20):
x = A*x + b
print (x)
Remark) try this one
Convergence check
In [13]:
# think about why this one does not work
A = np.array(([[3, 1, -1 ],
[4, 7, 1],
[2, -1, -4]]))
b = np.array([[7, 21, 15]]).T
# initial point
x = np.array([[1, 2, 2]]).T
for i in range(10):
x = A.dot(x) + b
print (x)
In [14]:
# stability, check eigenvalue of A
A = np.array(([[3, 1, -1 ],
[4, 7, 1],
[2, -1, -4]]))
np.linalg.eig(A)
Out[14]:
In [15]:
# stability, check eigenvalue of A
A = np.array(([[0, 1/4, -1/4 ],
[4/8, 0, 1/8],
[2/5, -1/5, 0]]))
np.linalg.eig(A)
Out[15]:
In [16]:
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')