|
4 | 4 | % function using fixed-point iteration. |
5 | 5 | % |
6 | 6 | % c = fixed_point_iteration(f,x0) |
7 | | -% c = fixed_point_iteration(f,x0,TOL) |
8 | | -% c = fixed_point_iteration(f,x0,[],imax) |
9 | | -% c = fixed_point_iteration(f,x0,TOL,imax) |
10 | | -% c = fixed_point_iteration(__,'all') |
| 7 | +% c = fixed_point_iteration(f,x0,opts) |
11 | 8 | % |
12 | 9 | % Copyright © 2021 Tamas Kis |
13 | | -% Last Update: 2021-07-25 |
14 | | -% Website: tamaskis.github.io |
| 10 | +% Last Update: 2021-08-28 |
| 11 | +% Website: https://tamaskis.github.io |
15 | 12 | % Contact: tamas.a.kis@outlook.com |
16 | 13 | % |
| 14 | +% TECHNICAL DOCUMENTATION: |
| 15 | +% https://tamaskis.github.io/documentation/Fixed_Point_Iteration.pdf |
| 16 | +% |
17 | 17 | % REFERENCES: |
18 | | -% [1] https://tamaskis.github.io/documentation/Fixed-Point%20Iteration.pdf |
| 18 | +% [1] Burden and Faires, "Numerical Analysis", 9th Ed. (pp. 56-66) |
19 | 19 | % |
20 | 20 | %-------------------------------------------------------------------------- |
21 | 21 | % |
22 | 22 | % ------ |
23 | 23 | % INPUT: |
24 | 24 | % ------ |
25 | 25 | % f - (function_handle) f(x) |
26 | | -% x0 - (1×1) initial guess for fixed point |
27 | | -% TOL - (OPTIONAL) (1×1) tolerance |
28 | | -% imax - (OPTIONAL) (1×1) maximum number of iterations |
29 | | -% output - (OPTIONAL) (char) if specified as 'all', function will return |
30 | | -% all intermediate fixed point estimates; otherwise, a faster |
31 | | -% algorithm is used to only return the converged fixed point |
| 26 | +% x0 - (1×1 double) initial guess for fixed point |
| 27 | +% opts - (OPTIONAL) (struct) solver options structure |
| 28 | +% • imax - (1×1 double) maximimum number of iterations |
| 29 | +% • return_all - (logical) all intermediate fixed point estimates |
| 30 | +% are returned if set to "true"; otherwise, a |
| 31 | +% faster algorithm is used to return only the |
| 32 | +% converged fixed point |
| 33 | +% • TOL - (1×1 double) tolerance |
| 34 | +% • warnings - (logical) true if any warnings should be |
| 35 | +% displayed, false if not |
32 | 36 | % |
33 | 37 | % ------- |
34 | 38 | % OUTPUT: |
35 | 39 | % ------- |
36 | | -% c - (1×1 or n×1) fixed point of f(x) |
37 | | -% --> if "output" is specified as 'all', then "c" will be a |
38 | | -% vector, where the first element is the initial guess, |
39 | | -% the last element is the converged fixed point, and the |
40 | | -% other elements are intermediate estimates of the fixed |
41 | | -% point |
42 | | -% --> otherwise, "c" is a single number storing the converged |
43 | | -% fixed point |
| 40 | +% c - (1×1 or n×1 double) fixed point of f(x) |
| 41 | +% --> If "return_all" is specified as "true", then "c" will |
| 42 | +% be a vector, where the first element is the initial |
| 43 | +% guess, the last element is the converged fixed point, |
| 44 | +% and the other elements are intermediate estimates of |
| 45 | +% the fixed point. |
| 46 | +% --> Otherwise, "c" is a single number storing the converged |
| 47 | +% fixed point. |
44 | 48 | % |
45 | 49 | %========================================================================== |
46 | | -function c = fixed_point_iteration(f,x0,TOL,imax,output) |
| 50 | +function c = fixed_point_iteration(f,x0,opts) |
47 | 51 |
|
48 | | - % sets default tolerance and maximum number of iterations if not |
49 | | - % specified by user |
50 | | - if (nargin < 3) || isempty(TOL) |
51 | | - TOL = 1e-12; |
52 | | - end |
53 | | - if (nargin < 4) || isempty(imax) |
| 52 | + % ---------------------------------- |
| 53 | + % Sets (or defaults) solver options. |
| 54 | + % ---------------------------------- |
| 55 | + |
| 56 | + % sets maximum number of iterations (defaults to 1e6) |
| 57 | + if (nargin < 3) || isempty(opts) || ~isfield(opts,'imax') |
54 | 58 | imax = 1e6; |
| 59 | + else |
| 60 | + imax = opts.imax; |
55 | 61 | end |
56 | 62 |
|
57 | | - % decides which algorithm to use |
58 | | - if nargin < 5 |
| 63 | + % determines return value (defaults to only return converged root) |
| 64 | + if (nargin < 3) || isempty(opts) || ~isfield(opts,'return_all') |
59 | 65 | return_all = false; |
60 | 66 | else |
61 | | - if strcmpi(output,'all') |
62 | | - return_all = true; |
63 | | - else |
64 | | - return_all = false; |
65 | | - end |
| 67 | + return_all = opts.return_all; |
| 68 | + end |
| 69 | + |
| 70 | + % sets tolerance (defaults to 1e-12) |
| 71 | + if (nargin < 3) || isempty(opts) || ~isfield(opts,'TOL') |
| 72 | + TOL = 1e-12; |
| 73 | + else |
| 74 | + TOL = opts.TOL; |
66 | 75 | end |
67 | 76 |
|
68 | | - % initializes the error so the loop will be entered |
69 | | - err = 2*TOL; |
| 77 | + % determines if warnings should be displayed (defaults to display) |
| 78 | + if (nargin < 3) || isempty(opts) || ~isfield(opts,'warnings') |
| 79 | + warnings = true; |
| 80 | + else |
| 81 | + warnings = opts.warnings; |
| 82 | + end |
| 83 | + |
| 84 | + % ----------------------------------------------------- |
| 85 | + % "Return all" implementation of fixed-point iteration. |
| 86 | + % ----------------------------------------------------- |
70 | 87 |
|
71 | | - % implements algorithm for fixed-point iteration where all intermediate |
72 | | - % fixed point estimates are also returned |
73 | 88 | if return_all |
74 | 89 |
|
75 | 90 | % preallocates x |
|
78 | 93 | % inputs initial guess for fixed point into x vector |
79 | 94 | x(1) = x0; |
80 | 95 |
|
| 96 | + % initializes the error so the loop will be entered |
| 97 | + err = 2*TOL; |
| 98 | + |
81 | 99 | % fixed-point iteration |
82 | 100 | i = 1; |
83 | 101 | while (err > TOL) && (i < imax) |
|
93 | 111 |
|
94 | 112 | end |
95 | 113 |
|
| 114 | + % displays warning if maximum number of iterations reached |
| 115 | + if (i == imax) && warnings |
| 116 | + warning(strcat('The method failed after n=',num2str(imax),... |
| 117 | + ' iterations.')); |
| 118 | + end |
| 119 | + |
96 | 120 | % returns converged fixed point along with intermediate fixed point |
97 | 121 | % estimates |
98 | 122 | c = x(1:i); |
99 | 123 |
|
| 124 | + % ----------------------------------------------- |
| 125 | + % "Fast" implementation of fixed-point iteration. |
| 126 | + % ----------------------------------------------- |
| 127 | + |
100 | 128 | else |
101 | 129 |
|
102 | 130 | % sets fixed point estimate at the first iteration of the fixed |
|
105 | 133 |
|
106 | 134 | % initializes x_new so its scope isn't limited to the while loop |
107 | 135 | x_new = 0; |
| 136 | + |
| 137 | + % initializes the error so the loop will be entered |
| 138 | + err = 2*TOL; |
108 | 139 |
|
109 | 140 | % fixed-point iteration |
110 | 141 | i = 1; |
|
124 | 155 |
|
125 | 156 | end |
126 | 157 |
|
| 158 | + % displays warning if maximum number of iterations reached |
| 159 | + if (i == imax) && warnings |
| 160 | + warning(strcat('The method failed after n=',num2str(imax),... |
| 161 | + ' iterations.')); |
| 162 | + end |
| 163 | + |
127 | 164 | % returns converged fixed point |
128 | 165 | c = x_new; |
129 | 166 |
|
|
0 commit comments