|
| 1 | +%========================================================================== |
| 2 | +% |
1 | 3 | % fixed_point_iteration Calculates the fixed point of a univariate
|
2 | 4 | % function using fixed-point iteration.
|
3 | 5 | %
|
4 |
| -% c = fixed_point_iteration(f,x0) returns the fixed point of a function |
5 |
| -% f(x) specified by the function handle f, where x0 is an initial guess |
6 |
| -% of the fixed point. The default tolerance and maximum number of |
7 |
| -% iterations are TOL = 1e-12 and imax = 1e6, respectively. |
8 |
| -% |
9 |
| -% c = fixed_point_iteration(f,x0,TOL) returns the fixed point of a |
10 |
| -% function f(x) specified by the function handle f, where x0 is an |
11 |
| -% initial guess of the fixed point and TOL is the tolerance. The default |
12 |
| -% maximum number of iterations is imax = 1e6. |
13 |
| -% |
14 |
| -% c = fixed_point_iteration(f,x0,[],imax) returns the fixed point of a |
15 |
| -% function f(x) specified by the function handle f, where x0 is an |
16 |
| -% initial guess of the fixed point and imax is the maximum number of |
17 |
| -% iterations. The default tolerance is TOL = 1e-12. |
| 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') |
18 | 11 | %
|
19 |
| -% c = fixed_point_iteration(f,x0,TOL,imax) returns the fixed point of a |
20 |
| -% function f(x) specified by the function handle f, where x0 is an |
21 |
| -% initial guess of the fixed point, TOL is the tolerance, and imax is the |
22 |
| -% maximum number of iterations. |
| 12 | +% Copyright © 2021 Tamas Kis |
| 13 | +% Last Update: 2021-06-18 |
23 | 14 | %
|
24 |
| -% c = fixed_point_iteration(__,'all') returns a vector, where the first |
25 |
| -% element of this vector is the initial guess, all intermediate elements |
26 |
| -% are the intermediate estimates of the fixed point, and the last element |
27 |
| -% is the converged fixed point. This identifier 'all' may be appended to |
28 |
| -% any of the syntaxes used above. |
| 15 | +%-------------------------------------------------------------------------- |
29 | 16 | %
|
30 | 17 | % MATLAB Central File Exchange: https://www.mathworks.com/matlabcentral/fileexchange/86992-fixed-point-iteration-fixed_point_iteration
|
31 | 18 | % GitHub: https://github.com/tamaskis/fixed_point_iteration-MATLAB
|
32 | 19 | %
|
33 |
| -% See "DOCUMENTATION.pdf" for additional documentation and examples. |
34 |
| -% Examples can also be found in EXAMPLES.m. Both of these files are |
35 |
| -% included with the download. |
| 20 | +% See EXAMPLES.mlx for examples and "DOCUMENTATION.pdf" for additional |
| 21 | +% documentation. Both of these files are included with the download. |
36 | 22 | %
|
37 |
| -% Copyright (c) 2021 Tamas Kis |
38 |
| -% Last Update: 2021-03-27 |
39 |
| - |
40 |
| - |
41 |
| - |
42 |
| -%% FUNCTION |
43 |
| - |
44 |
| -% INPUT: f - function handle for f(x) |
45 |
| -% x0 - initial guess for fixed point |
46 |
| -% TOL - (OPTIONAL) tolerance |
47 |
| -% imax - (OPTIONAL) maximum number of iterations |
48 |
| -% output - if specified as 'all', function will returns all |
49 |
| -% intermediate estimates of the fixed point; otherwise, a |
50 |
| -% faster algorithm is used to only return the converged |
51 |
| -% fixed point |
52 |
| -% OUTPUT: c - fixed point of f(x) |
53 |
| -% --> if "output" is specified as 'all', then "c" will be a |
54 |
| -% vector, where the first element is the initial guess, the |
55 |
| -% last element is the converged fixedd point, and the other |
56 |
| -% elements are intermediate estimates of the fixed point |
57 |
| -% --> otherwise, "c" is a single number storing the converged |
58 |
| -% fixed point |
| 23 | +%-------------------------------------------------------------------------- |
| 24 | +% |
| 25 | +% ------- |
| 26 | +% INPUTS: |
| 27 | +% ------- |
| 28 | +% f - (function_handle) f(x) |
| 29 | +% x0 - (1×1) initial guess for fixed point |
| 30 | +% TOL - (OPTIONAL) (1×1) tolerance |
| 31 | +% imax - (OPTIONAL) (1×1) maximum number of iterations |
| 32 | +% output - (OPTIONAL) (char) if specified as 'all', function will return |
| 33 | +% all intermediate fixed point estimates; otherwise, a faster |
| 34 | +% algorithm is used to only return the converged fixed point |
| 35 | +% |
| 36 | +% -------- |
| 37 | +% OUTPUTS: |
| 38 | +% -------- |
| 39 | +% c - (1×1 or n×1) fixed point of f(x) |
| 40 | +% --> if "output" is specified as 'all', then "c" will be a |
| 41 | +% vector, where the first element is the initial guess, |
| 42 | +% the last element is the converged fixed point, and the |
| 43 | +% other elements are intermediate estimates of the fixed |
| 44 | +% point |
| 45 | +% --> otherwise, "c" is a single number storing the converged |
| 46 | +% fixed point |
| 47 | +% |
| 48 | +%========================================================================== |
59 | 49 | function c = fixed_point_iteration(f,x0,TOL,imax,output)
|
60 | 50 |
|
61 | 51 | % sets default tolerance and maximum number of iterations if not
|
|
116 | 106 | % sets estimate of fixed point at the first iteration of the fixed
|
117 | 107 | % point iteration as the initial guess
|
118 | 108 | x_old = x0;
|
| 109 | + |
| 110 | + % initializes x_new so its scope isn't limited to the while loop |
| 111 | + x_new = 0; |
119 | 112 |
|
120 | 113 | % fixed-point iteration
|
121 | 114 | i = 1;
|
|
127 | 120 | % calculates error
|
128 | 121 | err = abs(x_new-x_old);
|
129 | 122 |
|
130 |
| - % stores updated estimate of fixed point for next iteration |
| 123 | + % stores current estimate of fixed point for next iteration |
131 | 124 | x_old = x_new;
|
132 | 125 |
|
133 | 126 | % increments loop index
|
134 | 127 | i = i+1;
|
135 | 128 |
|
136 | 129 | end
|
137 | 130 |
|
138 |
| - % returns fixed point |
139 |
| - c = x_old; |
| 131 | + % returns converged fixed point |
| 132 | + c = x_new; |
140 | 133 |
|
141 | 134 | end
|
142 | 135 |
|
|
0 commit comments