Skip to content

Commit da19574

Browse files
committed
Updated examples, function header, documentation, and readme.
1 parent fc1749e commit da19574

File tree

4 files changed

+51
-128
lines changed

4 files changed

+51
-128
lines changed

EXAMPLES.m

Lines changed: 0 additions & 71 deletions
This file was deleted.

EXAMPLES.mlx

57.5 KB
Binary file not shown.

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ Calculates the fixed point of a univariate function using fixed-point iteration.
1414

1515
## Description
1616

17-
`c = fixed_point_iteration(f,x0)` returns the fixed point of a function <img src="https://latex.codecogs.com/svg.latex?f(x)" title="f(x)" /> specified by the function handle `f`, where `x0` is an initial guess of the fixed point. The default tolerance and maximum number of iterations are `TOL = 1e-12` and `imax = 1e6`, respectively.
17+
`c = fixed_point_iteration(f,x0)` returns the fixed point of a function <img src="https://latex.codecogs.com/svg.latex?\inline&space;f(x)" title="f(x)" /> specified by the function handle `f`, where `x0` is an initial guess of the fixed point. The default tolerance and maximum number of iterations are `TOL = 1e-12` and `imax = 1e6`, respectively.
1818

19-
`c = fixed_point_iteration(f,x0,TOL)` returns the fixed point of a function <img src="https://latex.codecogs.com/svg.latex?f(x)" title="f(x)" /> specified by the function handle `f`, where `x0` is an initial guess of the fixed point and `TOL` is the tolerance. The default maximum number of iterations is `imax = 1e6`.
19+
`c = fixed_point_iteration(f,x0,TOL)` returns the fixed point of a function <img src="https://latex.codecogs.com/svg.latex?\inline&space;f(x)" title="f(x)" /> specified by the function handle `f`, where `x0` is an initial guess of the fixed point and `TOL` is the tolerance. The default maximum number of iterations is `imax = 1e6`.
2020

21-
`c = fixed_point_iteration(f,x0,[],imax)` returns the fixed point of a function <img src="https://latex.codecogs.com/svg.latex?f(x)" title="f(x)" /> specified by the function handle `f`, where `x0` is an initial guess of the fixed point and `imax` is the maximum number of iterations. The default tolerance is `TOL = 1e-12`.
21+
`c = fixed_point_iteration(f,x0,[],imax)` returns the fixed point of a function <img src="https://latex.codecogs.com/svg.latex?\inline&space;f(x)" title="f(x)" /> specified by the function handle `f`, where `x0` is an initial guess of the fixed point and `imax` is the maximum number of iterations. The default tolerance is `TOL = 1e-12`.
2222

23-
`c = fixed_point_iteration(f,x0,TOL,imax)` returns the fixed point of a function <img src="https://latex.codecogs.com/svg.latex?f(x)" title="f(x)" /> specified by the function handle `f`, where `x0` is an initial guess of the fixed point, `TOL` is the tolerance, and `imax` is the maximum number of iterations.
23+
`c = fixed_point_iteration(f,x0,TOL,imax)` returns the fixed point of a function <img src="https://latex.codecogs.com/svg.latex?\inline&space;f(x)" title="f(x)" /> specified by the function handle `f`, where `x0` is an initial guess of the fixed point, `TOL` is the tolerance, and `imax` is the maximum number of iterations.
2424

2525
`c = fixed_point_iteration(__,'all')` returns a vector, where the first element of this vector is the initial guess, all intermediate elements are the intermediate estimates of the fixed point, and the last element is the converged fixed point. This identifier 'all' may be appended to any of the syntaxes used above.
2626

2727

28-
## Additional Documentation and Examples
28+
## Examples and Additional Documentation
2929

30-
See "DOCUMENTATION.pdf" for additional documentation and examples.
30+
- See "EXAMPLES.mlx" or the "Examples" tab on the File Exchange page for examples.
31+
- See "DOCUMENTATION.pdf" (included with download, also available at https://github.com/tamaskis/fixed_point_iteration-MATLAB/blob/main/DOCUMENTATION.pdf) for additional documentation.

fixed_point_iteration.m

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,51 @@
1+
%==========================================================================
2+
%
13
% fixed_point_iteration Calculates the fixed point of a univariate
24
% function using fixed-point iteration.
35
%
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')
1811
%
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
2314
%
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+
%--------------------------------------------------------------------------
2916
%
3017
% MATLAB Central File Exchange: https://www.mathworks.com/matlabcentral/fileexchange/86992-fixed-point-iteration-fixed_point_iteration
3118
% GitHub: https://github.com/tamaskis/fixed_point_iteration-MATLAB
3219
%
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.
3622
%
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+
%==========================================================================
5949
function c = fixed_point_iteration(f,x0,TOL,imax,output)
6050

6151
% sets default tolerance and maximum number of iterations if not
@@ -116,6 +106,9 @@
116106
% sets estimate of fixed point at the first iteration of the fixed
117107
% point iteration as the initial guess
118108
x_old = x0;
109+
110+
% initializes x_new so its scope isn't limited to the while loop
111+
x_new = 0;
119112

120113
% fixed-point iteration
121114
i = 1;
@@ -127,16 +120,16 @@
127120
% calculates error
128121
err = abs(x_new-x_old);
129122

130-
% stores updated estimate of fixed point for next iteration
123+
% stores current estimate of fixed point for next iteration
131124
x_old = x_new;
132125

133126
% increments loop index
134127
i = i+1;
135128

136129
end
137130

138-
% returns fixed point
139-
c = x_old;
131+
% returns converged fixed point
132+
c = x_new;
140133

141134
end
142135

0 commit comments

Comments
 (0)