Created
November 8, 2022 15:12
-
-
Save ad-1/ed8815b804258072fa55538c89b6ae4c to your computer and use it in GitHub Desktop.
Fixed Point Iteration - Numerical Analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Method of computing fixed points of iterated functions. | |
| x = g(x0) => fixed points (input = output) | |
| """ | |
| def fixed_point_iteration(p0, tol, n): | |
| """ | |
| fixed point iteration algorithm for root finding | |
| param p0: initial guess for root | |
| param n: max number of iterations | |
| param tol: tolerance for root approximation | |
| """ | |
| i = 0 | |
| while i < n: | |
| # divergent: p = (p0 ** 2) - 1 # g(p0), g(x) = x**2 - 1 | |
| # division by zero : p = 1/(p0 - 1) | |
| p = 1 + 1/p0 | |
| if abs(p - p0) < tol: | |
| break | |
| i += 1 | |
| p0 = p | |
| print('root approximation = {}'.format(p)) | |
| # Program driver | |
| if __name__ == '__main__': | |
| # x**2 - x - 1 = 0 | |
| fixed_point_iteration(p0=1, tol=0.0001, n=20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment